src/stdlib/SDL_malloc.c
changeset 11610 6dea196ecbcb
parent 11601 22fe56a64c6f
child 11612 d42aebb99149
equal deleted inserted replaced
11607:db7ee6a1ba6a 11610:6dea196ecbcb
    24 #endif
    24 #endif
    25 
    25 
    26 #include "../SDL_internal.h"
    26 #include "../SDL_internal.h"
    27 
    27 
    28 /* This file contains portable memory management functions for SDL */
    28 /* This file contains portable memory management functions for SDL */
    29 
       
    30 #include "SDL_stdinc.h"
    29 #include "SDL_stdinc.h"
    31 
    30 #include "SDL_atomic.h"
    32 #if defined(HAVE_MALLOC)
    31 #include "SDL_error.h"
    33 
    32 
    34 void *SDL_malloc(size_t size)
    33 #ifndef HAVE_MALLOC
    35 {
       
    36     if (!size) {
       
    37         return malloc(1);
       
    38     }
       
    39     return malloc(size);
       
    40 }
       
    41 
       
    42 void *SDL_calloc(size_t nmemb, size_t size)
       
    43 {
       
    44     if (!size || !nmemb) {
       
    45         return calloc(1,1);
       
    46     }
       
    47     return calloc(nmemb, size);
       
    48 }
       
    49 
       
    50 void *SDL_realloc(void *ptr, size_t size)
       
    51 {
       
    52     return realloc(ptr, size);
       
    53 }
       
    54 
       
    55 void SDL_free(void *ptr)
       
    56 {
       
    57     free(ptr);
       
    58 }
       
    59 
       
    60 #else  /* the rest of this is a LOT of tapdancing to implement malloc. :) */
       
    61 
       
    62 #define LACKS_SYS_TYPES_H
    34 #define LACKS_SYS_TYPES_H
    63 #define LACKS_STDIO_H
    35 #define LACKS_STDIO_H
    64 #define LACKS_STRINGS_H
    36 #define LACKS_STRINGS_H
    65 #define LACKS_STRING_H
    37 #define LACKS_STRING_H
    66 #define LACKS_STDLIB_H
    38 #define LACKS_STDLIB_H
    67 #define ABORT
    39 #define ABORT
    68 #define USE_LOCKS 1
    40 #define USE_LOCKS 1
       
    41 #define USE_DL_PREFIX
    69 
    42 
    70 /*
    43 /*
    71   This is a version (aka dlmalloc) of malloc/free/realloc written by
    44   This is a version (aka dlmalloc) of malloc/free/realloc written by
    72   Doug Lea and released to the public domain, as explained at
    45   Doug Lea and released to the public domain, as explained at
    73   http://creativecommons.org/licenses/publicdomain.  Send questions,
    46   http://creativecommons.org/licenses/publicdomain.  Send questions,
   640 #endif /* NO_MALLINFO */
   613 #endif /* NO_MALLINFO */
   641 #ifndef MALLINFO_FIELD_TYPE
   614 #ifndef MALLINFO_FIELD_TYPE
   642 #define MALLINFO_FIELD_TYPE size_t
   615 #define MALLINFO_FIELD_TYPE size_t
   643 #endif /* MALLINFO_FIELD_TYPE */
   616 #endif /* MALLINFO_FIELD_TYPE */
   644 
   617 
       
   618 #ifndef memset
   645 #define memset  SDL_memset
   619 #define memset  SDL_memset
       
   620 #endif
       
   621 #ifndef memcpy
   646 #define memcpy  SDL_memcpy
   622 #define memcpy  SDL_memcpy
   647 #define malloc  SDL_malloc
   623 #endif
   648 #define calloc  SDL_calloc
       
   649 #define realloc SDL_realloc
       
   650 #define free    SDL_free
       
   651 
   624 
   652 /*
   625 /*
   653   mallopt tuning options.  SVID/XPG defines four standard parameter
   626   mallopt tuning options.  SVID/XPG defines four standard parameter
   654   numbers for mallopt, normally defined in malloc.h.  None of these
   627   numbers for mallopt, normally defined in malloc.h.  None of these
   655   are used in this malloc, so setting them has no effect. But this
   628   are used in this malloc, so setting them has no effect. But this
  5269 
  5242 
  5270 */
  5243 */
  5271 
  5244 
  5272 #endif /* !HAVE_MALLOC */
  5245 #endif /* !HAVE_MALLOC */
  5273 
  5246 
       
  5247 #ifdef HAVE_MALLOC
       
  5248 #define real_malloc malloc
       
  5249 #define real_calloc calloc
       
  5250 #define real_realloc realloc
       
  5251 #define real_free free
       
  5252 #else
       
  5253 #define real_malloc dlmalloc
       
  5254 #define real_calloc dlcalloc
       
  5255 #define real_realloc dlrealloc
       
  5256 #define real_free dlfree
       
  5257 #endif
       
  5258 
       
  5259 /* Memory functions used by SDL that can be replaced by the application */
       
  5260 static struct
       
  5261 {
       
  5262     SDL_malloc_func malloc_func;
       
  5263     SDL_calloc_func calloc_func;
       
  5264     SDL_realloc_func realloc_func;
       
  5265     SDL_free_func free_func;
       
  5266     SDL_atomic_t num_allocations;
       
  5267 } s_mem = {
       
  5268     real_malloc, real_calloc, real_realloc, real_free, { 0 }
       
  5269 };
       
  5270 
       
  5271 void SDL_GetMemoryFunctions(SDL_malloc_func *malloc_func,
       
  5272                             SDL_calloc_func *calloc_func,
       
  5273                             SDL_realloc_func *realloc_func,
       
  5274                             SDL_free_func *free_func)
       
  5275 {
       
  5276     if (malloc_func) {
       
  5277         *malloc_func = s_mem.malloc_func;
       
  5278     }
       
  5279     if (calloc_func) {
       
  5280         *calloc_func = s_mem.calloc_func;
       
  5281     }
       
  5282     if (realloc_func) {
       
  5283         *realloc_func = s_mem.realloc_func;
       
  5284     }
       
  5285     if (free_func) {
       
  5286         *free_func = s_mem.free_func;
       
  5287     }
       
  5288 }
       
  5289 
       
  5290 int SDL_SetMemoryFunctions(SDL_malloc_func malloc_func,
       
  5291                            SDL_calloc_func calloc_func,
       
  5292                            SDL_realloc_func realloc_func,
       
  5293                            SDL_free_func free_func)
       
  5294 {
       
  5295     if (!malloc_func) {
       
  5296         return SDL_InvalidParamError("malloc_func");
       
  5297     }
       
  5298     if (!calloc_func) {
       
  5299         return SDL_InvalidParamError("calloc_func");
       
  5300     }
       
  5301     if (!realloc_func) {
       
  5302         return SDL_InvalidParamError("realloc_func");
       
  5303     }
       
  5304     if (!free_func) {
       
  5305         return SDL_InvalidParamError("free_func");
       
  5306     }
       
  5307 
       
  5308     s_mem.malloc_func = malloc_func;
       
  5309     s_mem.calloc_func = calloc_func;
       
  5310     s_mem.realloc_func = realloc_func;
       
  5311     s_mem.free_func = free_func;
       
  5312     return 0;
       
  5313 }
       
  5314 
       
  5315 int SDL_GetNumAllocations()
       
  5316 {
       
  5317     return SDL_AtomicGet(&s_mem.num_allocations);
       
  5318 }
       
  5319 
       
  5320 void *SDL_malloc(size_t size)
       
  5321 {
       
  5322     void *mem;
       
  5323 
       
  5324     if (!size) {
       
  5325         size = 1;
       
  5326     }
       
  5327 
       
  5328     mem = s_mem.malloc_func(size);
       
  5329     if (mem) {
       
  5330         SDL_AtomicIncRef(&s_mem.num_allocations);
       
  5331     }
       
  5332     return mem;
       
  5333 }
       
  5334 
       
  5335 void *SDL_calloc(size_t nmemb, size_t size)
       
  5336 {
       
  5337     void *mem;
       
  5338 
       
  5339     if (!nmemb || !size) {
       
  5340         nmemb = 1;
       
  5341         size = 1;
       
  5342     }
       
  5343 
       
  5344     mem = s_mem.calloc_func(nmemb, size);
       
  5345     if (mem) {
       
  5346         SDL_AtomicIncRef(&s_mem.num_allocations);
       
  5347     }
       
  5348     return mem;
       
  5349 }
       
  5350 
       
  5351 void *SDL_realloc(void *ptr, size_t size)
       
  5352 {
       
  5353     void *mem;
       
  5354 
       
  5355     if (!ptr && !size) {
       
  5356         size = 1;
       
  5357     }
       
  5358 
       
  5359     mem = s_mem.realloc_func(ptr, size);
       
  5360     if (mem && !ptr) {
       
  5361         SDL_AtomicIncRef(&s_mem.num_allocations);
       
  5362     }
       
  5363     return mem;
       
  5364 }
       
  5365 
       
  5366 void SDL_free(void *ptr)
       
  5367 {
       
  5368     if (!ptr) {
       
  5369         return;
       
  5370     }
       
  5371 
       
  5372     s_mem.free_func(ptr);
       
  5373     SDL_AtomicDecRef(&s_mem.num_allocations);
       
  5374 }
       
  5375 
  5274 /* vi: set ts=4 sw=4 expandtab: */
  5376 /* vi: set ts=4 sw=4 expandtab: */