physfs_internal.h
changeset 852 9467e96abdf1
parent 851 dcfbb779ff40
child 857 5ecd1349c789
equal deleted inserted replaced
851:dcfbb779ff40 852:9467e96abdf1
    30 #endif
    30 #endif
    31 
    31 
    32 #ifdef __cplusplus
    32 #ifdef __cplusplus
    33 extern "C" {
    33 extern "C" {
    34 #endif
    34 #endif
       
    35 
       
    36 /*
       
    37  * Interface for small allocations. If you need a little scratch space for
       
    38  *  a throwaway buffer or string, use this. It will make small allocations
       
    39  *  on the stack if possible, and use allocator.Malloc() if they are too
       
    40  *  large. This helps reduce malloc pressure.
       
    41  * There are some rules, though:
       
    42  * NEVER return a pointer from this, as stack-allocated buffers go away
       
    43  *  when your function returns.
       
    44  * NEVER allocate in a loop, as stack-allocated pointers will pile up. Call
       
    45  *  a function that uses smallAlloc from your loop, so the allocation can
       
    46  *  free each time.
       
    47  * NEVER call smallAlloc with any complex expression (it's a macro that WILL
       
    48  *  have side effects...it references the argument multiple times). Use a
       
    49  *  variable or a literal.
       
    50  * NEVER free a pointer from this with anything but smallFree. It will not
       
    51  *  be a valid pointer to the allocator, regardless of where the memory came
       
    52  *  from.
       
    53  * NEVER realloc a pointer from this.
       
    54  * NEVER forget to use smallFree: it may not be a pointer from the stack.
       
    55  * NEVER forget to check for NULL...allocation can fail here, of course!
       
    56  */
       
    57 #define __PHYSFS_SMALLALLOCTHRESHOLD 128
       
    58 void *__PHYSFS_initSmallAlloc(void *ptr, PHYSFS_uint64 len);
       
    59 
       
    60 #define __PHYSFS_smallAlloc(bytes) ( \
       
    61     __PHYSFS_initSmallAlloc(((bytes < __PHYSFS_SMALLALLOCTHRESHOLD) ? \
       
    62                              alloca(bytes+1) : NULL), bytes) \
       
    63 )
       
    64 
       
    65 void __PHYSFS_smallFree(void *ptr);
       
    66 
    35 
    67 
    36 /* Use the allocation hooks. */
    68 /* Use the allocation hooks. */
    37 #define malloc(x) Do not use malloc() directly.
    69 #define malloc(x) Do not use malloc() directly.
    38 #define realloc(x, y) Do not use realloc() directly.
    70 #define realloc(x, y) Do not use realloc() directly.
    39 #define free(x) Do not use free() directly.
    71 #define free(x) Do not use free() directly.