Ticket #1402: PCA-patch-002.patch

File PCA-patch-002.patch, 7.3 KB (added by Paul C. Anagnostopoulos, 11 years ago)
  • src/gc/alloc_memory.c

     
    6464 
    6565/* 
    6666 
    67 =item C<void * mem__internal_allocate(size_t size, const char *file, int line)> 
    68  
    69 Calls C<malloc> to allocate memory from the system, Panics if there is no 
    70 memory available. If C<DETAIL_MEMORY_DEBUG> macro is defined, prints 
    71 debug information to C<STDERR>. 
    72  
    73 =cut 
    74  
    75 */ 
    76  
    77 PARROT_MALLOC 
    78 PARROT_CANNOT_RETURN_NULL 
    79 void * 
    80 mem__internal_allocate(size_t size, ARGIN(const char *file), int line) 
    81 { 
    82     ASSERT_ARGS(mem__internal_allocate) 
    83     void * const ptr = malloc((size_t)size); 
    84 #ifdef DETAIL_MEMORY_DEBUG 
    85     fprintf(stderr, "Internal malloc %i at %p (%s/%d)\n", 
    86             size, ptr, file, line); 
    87 #else 
    88     UNUSED(file); 
    89     UNUSED(line); 
    90 #endif 
    91     if (!ptr) 
    92         PANIC_OUT_OF_MEM(size); 
    93     return ptr; 
    94 } 
    95  
    96 /* 
    97  
    9867=item C<void * mem_sys_allocate_zeroed(size_t size)> 
    9968 
    10069Uses C<calloc> to allocate system memory.  Guaranteed to succeed, Panics 
     
    12291 
    12392/* 
    12493 
    125 =item C<void * mem__internal_allocate_zeroed(size_t size, const char *file, int 
    126 line)> 
    127  
    128 Uses C<calloc> to allocate system memory.  Guaranteed to succeed, Panics 
    129 otherwise. If C<DETAIL_MEMORY_DEBUG> macro is defined, prints 
    130 debug information to C<STDERR>. 
    131  
    132 =cut 
    133  
    134 */ 
    135  
    136 PARROT_MALLOC 
    137 PARROT_CANNOT_RETURN_NULL 
    138 void * 
    139 mem__internal_allocate_zeroed(size_t size, ARGIN(const char *file), int line) 
    140 { 
    141     ASSERT_ARGS(mem__internal_allocate_zeroed) 
    142     void * const ptr = calloc(1, (size_t)size); 
    143 #ifdef DETAIL_MEMORY_DEBUG 
    144     fprintf(stderr, "Internal malloc %i at %p (%s/%d)\n", 
    145             size, ptr, file, line); 
    146 #else 
    147     UNUSED(file); 
    148     UNUSED(line); 
    149 #endif 
    150     if (!ptr) 
    151         PANIC_OUT_OF_MEM(size); 
    152     return ptr; 
    153 } 
    154  
    155 /* 
    156  
    15794=item C<void * mem_sys_realloc(void *from, size_t size)> 
    15895 
    15996Resizes a chunk of memory.  Unlike C<realloc>, it can handle a 
     
    226163 
    227164/* 
    228165 
    229 =item C<void * mem__internal_realloc(void *from, size_t size, const char *file, 
    230 int line)> 
    231  
    232 Resizes a chunk of system memory.  Unlike C<realloc>, it can handle a 
    233 NULL pointer, in which case a new memory block is allocated for the 
    234 requested size. If C<DETAIL_MEMORY_DEBUG> macro is defined, debug 
    235 information is printed to C<STDERR>. 
    236  
    237 =cut 
    238  
    239 */ 
    240  
    241 PARROT_MALLOC 
    242 PARROT_CANNOT_RETURN_NULL 
    243 void * 
    244 mem__internal_realloc(ARGFREE(void *from), size_t size, 
    245         ARGIN(const char *file), int line) 
    246 { 
    247     ASSERT_ARGS(mem__internal_realloc) 
    248     void * const ptr = realloc(from, size); 
    249 #ifdef DETAIL_MEMORY_DEBUG 
    250     fprintf(stderr, "internal free of %p (realloc -- %i bytes) (%s/%d)\n", 
    251             from, size, file, line); 
    252     fprintf(stderr, "Internal malloc %i at %p (%s/%d)\n", 
    253             size, ptr, file, line); 
    254 #else 
    255     UNUSED(file); 
    256     UNUSED(line); 
    257 #endif 
    258     if (!ptr) 
    259         PANIC_OUT_OF_MEM(size); 
    260     return ptr; 
    261 } 
    262  
    263 /* 
    264  
    265 =item C<void * mem__internal_realloc_zeroed(void *from, size_t size, size_t 
    266 old_size, const char *file, int line)> 
    267  
    268 Reallocates a given buffer of size C<old_size> to C<size>. If the new size 
    269 is larger then the old size, the difference is filled with zeros. Contains 
    270 debugging information, and can print filename and line number where it is 
    271 used if C<DETAIL_MEMORY_DEBUG> is defined. 
    272  
    273 =cut 
    274  
    275 */ 
    276  
    277 PARROT_MALLOC 
    278 PARROT_CANNOT_RETURN_NULL 
    279 void * 
    280 mem__internal_realloc_zeroed(ARGFREE(void *from), size_t size, size_t old_size, 
    281     ARGIN(const char *file), int line) 
    282 { 
    283     ASSERT_ARGS(mem__internal_realloc_zeroed) 
    284     void * const ptr = realloc(from, size); 
    285 #ifdef DETAIL_MEMORY_DEBUG 
    286     fprintf(stderr, "internal free of %p (realloc -- %i bytes) (%s/%d)\n", 
    287             from, size, file, line); 
    288     fprintf(stderr, "Internal malloc %i at %p (%s/%d)\n", 
    289             size, ptr, file, line); 
    290 #else 
    291     UNUSED(file); 
    292     UNUSED(line); 
    293 #endif 
    294     if (!ptr) 
    295         PANIC_OUT_OF_MEM(size); 
    296     if (size > old_size) 
    297         memset((char*)ptr + old_size, 0, size - old_size); 
    298  
    299     return ptr; 
    300 } 
    301  
    302 /* 
    303  
    304166=item C<void mem_sys_free(void *from)> 
    305167 
    306168Frees a chunk of memory back to the system. 
     
    323185 
    324186/* 
    325187 
    326 =item C<void mem__internal_free(void *from, const char *file, int line)> 
    327  
    328 Frees a chunk of memory back to the system. If 
    329 C<DETAIL_MEMORY_DEBUG> macro is defined, prints debug information to 
    330 C<STDERR>. 
    331  
    332 =cut 
    333  
    334 */ 
    335  
    336 void 
    337 mem__internal_free(ARGFREE(void *from), ARGIN(const char *file), int line) 
    338 { 
    339     ASSERT_ARGS(mem__internal_free) 
    340 #ifdef DETAIL_MEMORY_DEBUG 
    341     fprintf(stderr, "Internal free of %p (%s/%d)\n", from, file, line); 
    342 #else 
    343     UNUSED(file); 
    344     UNUSED(line); 
    345 #endif 
    346     free(from); 
    347 } 
    348  
    349 /* 
    350  
    351188=item C<char * mem_sys_strdup(const char *src)> 
    352189 
    353190Copy a C string to a new block of memory allocated with mem_sys_allocate, 
  • include/parrot/memory.h

     
    1717/* Use these macros instead of calling the functions listed below. */ 
    1818/* They protect against things like passing null to mem__sys_realloc, */ 
    1919/* which is not portable. */ 
    20 #define mem_internal_allocate(x) mem__internal_allocate((x), __FILE__, __LINE__) 
    21 #define mem_internal_allocate_typed(type) \ 
    22     (type *)mem__internal_allocate(sizeof (type), __FILE__, __LINE__) 
    23 #define mem_internal_allocate_zeroed(x) mem__internal_allocate_zeroed((x), \ 
    24     __FILE__, __LINE__) 
     20 
     21/* It was decided that having a second set of memory allocation functions 
     22   is a waste. These macros were rewritten to use the much more prevalent 
     23   mem_sys_* functions and then the mem__internal_* functions were 
     24   eliminated. */ 
     25 
     26#define mem_internal_allocate(x) mem_sys_allocate(x) 
     27 
     28#define mem_internal_allocate_typed(type) (type *)mem_sys_allocate(sizeof (type)) 
     29 
     30#define mem_internal_allocate_zeroed(x) mem_sys_allocate_zeroed(x) 
     31 
    2532#define mem_internal_allocate_zeroed_typed(type) \ 
    26     (type *)mem__internal_allocate_zeroed(sizeof (type), __FILE__, __LINE__) 
     33    (type *)mem_sys_allocate_zeroed(sizeof (type)) 
     34 
    2735#define mem_internal_allocate_n_zeroed_typed(n, type) \ 
    28     (type *)mem__internal_allocate_zeroed((n) * sizeof (type), __FILE__, __LINE__) 
     36    (type *)mem_sys_allocate_zeroed((n) * sizeof (type)) 
    2937 
    30 #define mem_internal_realloc(x, y) mem__internal_realloc((x), (y), __FILE__, __LINE__) 
    31 #define mem_internal_realloc_zeroed(p, x, y) mem__internal_realloc_zeroed((p), (x), (y), __FILE__, __LINE__) 
    32 #define mem_internal_realloc_n_zeroed_typed(p, x, y, type) (type *)mem__internal_realloc_zeroed((p), (x) * sizeof (type), (y) * sizeof (type), __FILE__, __LINE__) 
    33 #define mem_internal_free(x) mem__internal_free((x), __FILE__, __LINE__) 
     38#define mem_internal_realloc(x, y) mem_sys_realloc((x), (y)) 
    3439 
     40#define mem_internal_realloc_zeroed(p, x, y) mem_sys_realloc_zeroed((p), (x), (y)) 
     41 
     42#define mem_internal_realloc_n_zeroed_typed(p, x, y, type) \ 
     43  (type *)mem_sys_realloc_zeroed((p), (x) * sizeof (type), (y) * sizeof (type)) 
     44 
     45#define mem_internal_free(x) mem_sys_free(x) 
     46 
     47 
    3548#define mem_sys_memcopy memcpy 
    3649#define mem_sys_memmove memmove 
    3750