fix MSVC-ARM internal compiler error by wrapping malloc
Before adoption: this commit came in with the history the project adopted, so no landing records it.
For some reason Microsoft CL compiler for ARM is unable to compile malloc when parameter is an expression. malloc(x * y) will cause internal compiler error, malloc(x) is however fine.
authored by Michał Cichoń · committed by Michał Cichoń · parent ee8e926 · Browse files · 1 file·+31 added−22 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+31 added−22 removed
- other:+31 added−22 removed
Provenance
- Commit
10def9b- Tree
aebe8a8- Author
Michał Cichoń <michcic@gmail.com>- Committer
Michał Cichoń <michcic@gmail.com>- Parent
ee8e926
1 file changed+31 added−22 removed
modified:stb_image.h+31 added-22 removed
1-/* stb_image - v1.43 - public domain JPEG/PNG reader - http://nothings.org/stb_image.c1+/* stb_image - v1.44 - public domain JPEG/PNG reader - http://nothings.org/stb_image.c2 when you control the images you're loading3 no warranty implied; use at your own risk4 5 Do this:21 unmodified lines27 - decode from arbitrary I/O callbacks28 - overridable dequantizing-IDCT, YCbCr-to-RGB conversion (define STBI_SIMD)29 30 Latest revisions:31+ 1.44 (2014-08-04) fix MSVC-ARM internal compiler error by wrapping malloc32 1.43 (2014-07-15) fix MSVC-only bug in 1.4233 1.42 (2014-07-09) no _CRT_SECURE_NO_WARNINGS; error-path fixes; STBI_ASSERT34 1.41 (2014-06-25) fix search&replace that messed up comments/error messages35 1.40 (2014-06-22) gcc warning35 unmodified lines71 isn't, let Sean know. Guillaume George72 Jerry Jansson73 Hayaki Saito74 Johan Duparc75+ Michal Cichon76 */77 78 #ifndef STBI_INCLUDE_STB_IMAGE_H79 #define STBI_INCLUDE_STB_IMAGE_H446 unmodified lines526 stbi__g_failure_reason = str;527 return 0;528 }529 530+static void *stbi__malloc(size_t size)531+{532+ return malloc(size);533+}534+535 // stbi__err - error536 // stbi__errpf - error returning pointer to float537 // stbi__errpuc - error returning pointer to unsigned char538 342 unmodified lines881 882 if (req_comp == img_n) return data;883 STBI_ASSERT(req_comp >= 1 && req_comp <= 4);884 878- good = (unsigned char *) malloc(req_comp * x * y);885+ good = (unsigned char *) stbi__malloc(req_comp * x * y);886 if (good == NULL) {887 free(data);888 return stbi__errpuc("outofmem", "Out of memory");889 }31 unmodified lines921 #ifndef STBI_NO_HDR922 static float *stbi__ldr_to_hdr(stbi_uc *data, int x, int y, int comp)923 {924 int i,k,n;918- float *output = (float *) malloc(x * y * comp * sizeof(float));925+ float *output = (float *) stbi__malloc(x * y * comp * sizeof(float));926 if (output == NULL) { free(data); return stbi__errpf("outofmem", "Out of memory"); }927 // compute number of non-alpha components928 if (comp & 1) n = comp; else n = comp-1;929 for (i=0; i < x*y; ++i) {9 unmodified lines939 #define stbi__float2int(x) ((int) (x))940 static stbi_uc *stbi__hdr_to_ldr(float *data, int x, int y, int comp)941 {942 int i,k,n;936- stbi_uc *output = (stbi_uc *) malloc(x * y * comp);943+ stbi_uc *output = (stbi_uc *) stbi__malloc(x * y * comp);944 if (output == NULL) { free(data); return stbi__errpuc("outofmem", "Out of memory"); }945 // compute number of non-alpha components946 if (comp & 1) n = comp; else n = comp-1;947 for (i=0; i < x*y; ++i) {668 unmodified lines1616 // big blocks (stbi__err.g. a 16x16 iMCU on an image of width 33); we won't1617 // discard the extra data until colorspace conversion1618 z->img_comp[i].w2 = z->img_mcu_x * z->img_comp[i].h * 8;1619 z->img_comp[i].h2 = z->img_mcu_y * z->img_comp[i].v * 8;1613- z->img_comp[i].raw_data = malloc(z->img_comp[i].w2 * z->img_comp[i].h2+15);1620+ z->img_comp[i].raw_data = stbi__malloc(z->img_comp[i].w2 * z->img_comp[i].h2+15);1621 if (z->img_comp[i].raw_data == NULL) {1622 for(--i; i >= 0; --i) {1623 free(z->img_comp[i].raw_data);1624 z->img_comp[i].data = NULL;256 unmodified lines1881 stbi__resample *r = &res_comp[k];1882 1883 // allocate line buffer big enough for upsampling off the edges1884 // with upsample factor of 41878- z->img_comp[k].linebuf = (stbi_uc *) malloc(z->s->img_x + 3);1885+ z->img_comp[k].linebuf = (stbi_uc *) stbi__malloc(z->s->img_x + 3);1886 if (!z->img_comp[k].linebuf) { stbi__cleanup_jpeg(z); return stbi__errpuc("outofmem", "Out of memory"); }1887 1888 r->hs = z->img_h_max / z->img_comp[k].h;1889 r->vs = z->img_v_max / z->img_comp[k].v;9 unmodified lines1899 else r->resample = stbi__resample_row_generic;1900 }1901 1902 // can't error after this so, this is safe1896- output = (stbi_uc *) malloc(n * z->s->img_x * z->s->img_y + 1);1903+ output = (stbi_uc *) stbi__malloc(n * z->s->img_x * z->s->img_y + 1);1904 if (!output) { stbi__cleanup_jpeg(z); return stbi__errpuc("outofmem", "Out of memory"); }1905 1906 // now go ahead and resample1907 for (j=0; j < z->s->img_y; ++j) {436 unmodified lines2344 2345 STBIDEF char *stbi_zlib_decode_malloc_guesssize(const char *buffer, int len, int initial_size, int *outlen)2346 {2347 stbi__zbuf a;2341- char *p = (char *) malloc(initial_size);2348+ char *p = (char *) stbi__malloc(initial_size);2349 if (p == NULL) return NULL;2350 a.zbuffer = (stbi_uc *) buffer;2351 a.zbuffer_end = (stbi_uc *) buffer + len;2352 if (stbi__do_zlib(&a, p, initial_size, 1, 1)) {12 unmodified lines2365 2366 STBIDEF char *stbi_zlib_decode_malloc_guesssize_headerflag(const char *buffer, int len, int initial_size, int *outlen, int parse_header)2367 {2368 stbi__zbuf a;2362- char *p = (char *) malloc(initial_size);2369+ char *p = (char *) stbi__malloc(initial_size);2370 if (p == NULL) return NULL;2371 a.zbuffer = (stbi_uc *) buffer;2372 a.zbuffer_end = (stbi_uc *) buffer + len;2373 if (stbi__do_zlib(&a, p, initial_size, 1, parse_header)) {18 unmodified lines2392 2393 STBIDEF char *stbi_zlib_decode_noheader_malloc(char const *buffer, int len, int *outlen)2394 {2395 stbi__zbuf a;2389- char *p = (char *) malloc(16384);2396+ char *p = (char *) stbi__malloc(16384);2397 if (p == NULL) return NULL;2398 a.zbuffer = (stbi_uc *) buffer;2399 a.zbuffer_end = (stbi_uc *) buffer+len;2400 if (stbi__do_zlib(&a, p, 16384, 1, 0)) {89 unmodified lines2490 stbi__uint32 i,j,stride = x*out_n;2491 int k;2492 int img_n = s->img_n; // copy it into a local for later2493 STBI_ASSERT(out_n == s->img_n || out_n == s->img_n+1);2487- a->out = (stbi_uc *) malloc(x * y * out_n);2494+ a->out = (stbi_uc *) stbi__malloc(x * y * out_n);2495 if (!a->out) return stbi__err("outofmem", "Out of memory");2496 if (s->img_x == x && s->img_y == y) {2497 if (raw_len != (img_n * x + 1) * y) return stbi__err("not enough pixels","Corrupt PNG");2498 } else { // interlaced:66 unmodified lines2565 if (!interlaced)2566 return stbi__create_png_image_raw(a, raw, raw_len, out_n, a->s->img_x, a->s->img_y);2567 2568 // de-interlacing2562- final = (stbi_uc *) malloc(a->s->img_x * a->s->img_y * out_n);2569+ final = (stbi_uc *) stbi__malloc(a->s->img_x * a->s->img_y * out_n);2570 for (p=0; p < 7; ++p) {2571 int xorig[] = { 0,4,0,2,0,1,0 };2572 int yorig[] = { 0,0,4,0,2,0,1 };2573 int xspc[] = { 8,8,4,4,2,2,1 };50 unmodified lines2624 {2625 stbi__uint32 i, pixel_count = a->s->img_x * a->s->img_y;2626 stbi_uc *p, *temp_out, *orig = a->out;2627 2621- p = (stbi_uc *) malloc(pixel_count * pal_img_n);2628+ p = (stbi_uc *) stbi__malloc(pixel_count * pal_img_n);2629 if (p == NULL) return stbi__err("outofmem", "Out of memory");2630 2631 // between here and free(out) below, exitting would leak2632 temp_out = p;447 unmodified lines3080 if (req_comp && req_comp >= 3) // we can directly decode 3 or 43081 target = req_comp;3082 else3083 target = s->img_n; // if they want monochrome, we'll post-convert3077- out = (stbi_uc *) malloc(target * s->img_x * s->img_y);3084+ out = (stbi_uc *) stbi__malloc(target * s->img_x * s->img_y);3085 if (!out) return stbi__errpuc("outofmem", "Out of memory");3086 if (bpp < 16) {3087 int z=0;3088 if (psize == 0 || psize > 256) { free(out); return stbi__errpuc("invalid", "Corrupt BMP"); }220 unmodified lines3309 *x = tga_width;3310 *y = tga_height;3311 if (comp) *comp = tga_comp;3312 3306- tga_data = (unsigned char*)malloc( tga_width * tga_height * tga_comp );3313+ tga_data = (unsigned char*)stbi__malloc( tga_width * tga_height * tga_comp );3314 if (!tga_data) return stbi__errpuc("outofmem", "Out of memory");3315 3316 // skip to the data's starting position (offset usually = 0)3317 stbi__skip(s, tga_offset );10 unmodified lines3328 {3329 // any data to skip? (offset usually = 0)3330 stbi__skip(s, tga_palette_start );3331 // load the palette3325- tga_palette = (unsigned char*)malloc( tga_palette_len * tga_palette_bits / 8 );3332+ tga_palette = (unsigned char*)stbi__malloc( tga_palette_len * tga_palette_bits / 8 );3333 if (!tga_palette) {3334 free(tga_data);3335 return stbi__errpuc("outofmem", "Out of memory");3336 }182 unmodified lines3519 if (compression > 1)3520 return stbi__errpuc("bad compression", "PSD has an unknown compression format");3521 3522 // Create the destination image.3516- out = (stbi_uc *) malloc(4 * w*h);3523+ out = (stbi_uc *) stbi__malloc(4 * w*h);3524 if (!out) return stbi__errpuc("outofmem", "Out of memory");3525 pixelCount = w*h;3526 3527 // Initialize the data to zero.276 unmodified lines3804 stbi__get16be(s); //skip `fields'3805 stbi__get16be(s); //skip `pad'3806 3807 // intermediate buffer is RGBA3801- result = (stbi_uc *) malloc(x*y*4);3808+ result = (stbi_uc *) stbi__malloc(x*y*4);3809 memset(result, 0xff, x*y*4);3810 3811 if (!stbi__pic_load_core(s,x,y,comp, result)) {3812 free(result);242 unmodified lines4055 stbi_uc *old_out = 0;4056 4057 if (g->out == 0) {4058 if (!stbi__gif_header(s, g, comp,0)) return 0; // stbi__g_failure_reason set by stbi__gif_header4052- g->out = (stbi_uc *) malloc(4 * g->w * g->h);4059+ g->out = (stbi_uc *) stbi__malloc(4 * g->w * g->h);4060 if (g->out == 0) return stbi__errpuc("outofmem", "Out of memory");4061 stbi__fill_gif_background(g);4062 } else {4063 // animated-gif-only path4064 if (((g->eflags & 0x1C) >> 2) == 3) {4065 old_out = g->out;4059- g->out = (stbi_uc *) malloc(4 * g->w * g->h);4066+ g->out = (stbi_uc *) stbi__malloc(4 * g->w * g->h);4067 if (g->out == 0) return stbi__errpuc("outofmem", "Out of memory");4068 memcpy(g->out, old_out, g->w*g->h*4);4069 }4070 }214 unmodified lines4285 if (comp) *comp = 3;4286 if (req_comp == 0) req_comp = 3;4287 4288 // Read data4282- hdr_data = (float *) malloc(height * width * req_comp * sizeof(float));4289+ hdr_data = (float *) stbi__malloc(height * width * req_comp * sizeof(float));4290 4291 // Load image data4292 // image data is stored as some number of sca4293 if ( width < 8 || width >= 32768) {30 unmodified lines4324 }4325 len <<= 8;4326 len |= stbi__get8(s);4327 if (len != width) { free(hdr_data); free(scanline); return stbi__errpf("invalid decoded scanline length", "corrupt HDR"); }4321- if (scanline == NULL) scanline = (stbi_uc *) malloc(width * 4);4328+ if (scanline == NULL) scanline = (stbi_uc *) stbi__malloc(width * 4);4329 4330 for (k = 0; k < 4; ++k) {4331 i = 0;4332 while (i < width) {228 unmodified lines4561 #endif // STB_IMAGE_IMPLEMENTATION4562 4563 /*4564 revision history:4565+ 1.44 (2014-08-04)4566+ fix MSVC-ARM internal compiler error by wrapping malloc4567 1.43 (2014-07-15)4568 fix MSVC-only compiler problem in code changed in 1.424569 1.42 (2014-07-09)4570 don't define _CRT_SECURE_NO_WARNINGS (affects user code)110 unmodified lines