home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 2 / AACD 2.iso / AACD / Magazine / UsingPDF / GhostScript / source / gs5.10 / gsmemory.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-10-02  |  12.4 KB  |  419 lines

  1. /* Copyright (C) 1993, 1996, 1997 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of Aladdin Ghostscript.
  4.   
  5.   Aladdin Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author
  6.   or distributor accepts any responsibility for the consequences of using it,
  7.   or for whether it serves any particular purpose or works at all, unless he
  8.   or she says so in writing.  Refer to the Aladdin Ghostscript Free Public
  9.   License (the "License") for full details.
  10.   
  11.   Every copy of Aladdin Ghostscript must include a copy of the License,
  12.   normally in a plain ASCII text file named PUBLIC.  The License grants you
  13.   the right to copy, modify and redistribute Aladdin Ghostscript, but only
  14.   under certain conditions described in the License.  Among other things, the
  15.   License requires that the copyright notice and this notice be preserved on
  16.   all copies.
  17. */
  18.  
  19. /* gsmemory.c */
  20. /* Generic allocator support for Ghostscript library */
  21. #include "gx.h"
  22. #include "malloc_.h"
  23. #include "memory_.h"
  24. #include "gsmdebug.h"
  25. #include "gsrefct.h"        /* to check prototype */
  26. #include "gsstruct.h"        /* ditto */
  27.  
  28. /* Define the fill patterns for unallocated memory. */
  29. byte gs_alloc_fill_alloc = 0xa1;
  30. byte gs_alloc_fill_block = 0xb1;
  31. byte gs_alloc_fill_collected = 0xc1;
  32. byte gs_alloc_fill_deleted = 0xd1;
  33. byte gs_alloc_fill_free = 0xf1;
  34.  
  35. /* A 'structure' type descriptor for free blocks. */
  36. gs_public_st_simple(st_free, byte, "(free)");
  37.  
  38. /* The 'structure' type descriptor for bytes. */
  39. gs_public_st_simple(st_bytes, byte, "bytes");
  40.  
  41. /* The descriptors for elements and arrays of const strings. */
  42. private_st_const_string();
  43. public_st_const_string_element();
  44. #define sptr ((gs_const_string *)vptr)
  45. private ENUM_PTRS_BEGIN(const_string_enum_ptrs) return 0;
  46.     case 0: *pep = (void *)sptr; return ptr_const_string_type;
  47. ENUM_PTRS_END
  48. private RELOC_PTRS_BEGIN(const_string_reloc_ptrs) {
  49.     gs_reloc_const_string(sptr, gcst);
  50. } RELOC_PTRS_END
  51. #undef sptr
  52.  
  53. /* Fill an unoccupied block with a pattern. */
  54. /* Note that the block size may be too large for a single memset. */
  55. void
  56. gs_alloc_memset(void *ptr, int/*byte*/ fill, ulong lsize)
  57. {    ulong msize = lsize;
  58.     char *p = ptr;
  59.     int isize;
  60.  
  61.     for ( ; msize; msize -= isize, p += isize )
  62.       { isize = min(msize, max_int);
  63.         memset(p, fill, isize);
  64.       }
  65. }
  66.  
  67. /* ------ Heap allocator ------ */
  68.  
  69. /*
  70.  * An implementation of Ghostscript's memory manager interface
  71.  * that works directly with the C heap.  We keep track of all allocated
  72.  * blocks so we can free them at cleanup time.
  73.  */
  74. private gs_memory_proc_alloc_bytes(gs_heap_alloc_bytes);
  75. private gs_memory_proc_alloc_struct(gs_heap_alloc_struct);
  76. private gs_memory_proc_alloc_byte_array(gs_heap_alloc_byte_array);
  77. private gs_memory_proc_alloc_struct_array(gs_heap_alloc_struct_array);
  78. private gs_memory_proc_resize_object(gs_heap_resize_object);
  79. private gs_memory_proc_object_size(gs_heap_object_size);
  80. private gs_memory_proc_object_type(gs_heap_object_type);
  81. private gs_memory_proc_free_object(gs_heap_free_object);
  82. private gs_memory_proc_alloc_string(gs_heap_alloc_string);
  83. private gs_memory_proc_resize_string(gs_heap_resize_string);
  84. private gs_memory_proc_free_string(gs_heap_free_string);
  85. private gs_memory_proc_register_root(gs_heap_register_root);
  86. private gs_memory_proc_unregister_root(gs_heap_unregister_root);
  87. private gs_memory_proc_status(gs_heap_status);
  88. private gs_memory_proc_enable_free(gs_heap_enable_free);
  89. gs_memory_t gs_memory_default = {
  90.     {    gs_heap_alloc_bytes,
  91.         gs_heap_alloc_bytes,
  92.         gs_heap_alloc_struct,
  93.         gs_heap_alloc_struct,
  94.         gs_heap_alloc_byte_array,
  95.         gs_heap_alloc_byte_array,
  96.         gs_heap_alloc_struct_array,
  97.         gs_heap_alloc_struct_array,
  98.         gs_heap_resize_object,
  99.         gs_heap_object_size,
  100.         gs_heap_object_type,
  101.         gs_heap_free_object,
  102.         gs_heap_alloc_string,
  103.         gs_heap_alloc_string,
  104.         gs_heap_resize_string,
  105.         gs_heap_free_string,
  106.         gs_heap_register_root,
  107.         gs_heap_unregister_root,
  108.         gs_heap_status,
  109.         gs_heap_enable_free
  110.     }
  111. };
  112. /* We must make sure that malloc_blocks leave the block aligned. */
  113. typedef struct malloc_block_s malloc_block;
  114. #define malloc_block_data\
  115.     malloc_block *next;\
  116.     malloc_block *prev;\
  117.     uint size;\
  118.     gs_memory_type_ptr_t type;\
  119.     client_name_t cname
  120. struct malloc_block_data_s { malloc_block_data; };
  121. struct malloc_block_s {
  122.     malloc_block_data;
  123. /* ANSI C does not allow zero-size arrays, so we need the following */
  124. /* unnecessary and wasteful workaround: */
  125. #define _npad (-size_of(struct malloc_block_data_s) & 7)
  126.     byte _pad[(_npad == 0 ? 8 : _npad)];    /* pad to double */
  127. #undef _npad
  128. };
  129.  
  130. private malloc_block *malloc_list;
  131. private long malloc_used;
  132. /* The record of maximum allocation is public. */
  133. long gs_malloc_max = 0;
  134. /* The limit on allocable space is public. */
  135. long gs_malloc_limit = max_long;
  136.  
  137. /* Initialize the malloc heap. */
  138. private long heap_available(P0());
  139. void
  140. gs_malloc_init(void)
  141. {    malloc_list = 0;
  142.     malloc_used = 0;
  143.     gs_malloc_max = 0;
  144. }
  145. /* Estimate the amount of available memory by probing with mallocs. */
  146. /* We may under-estimate by a lot, but that's better than winding up with */
  147. /* a seriously inflated address space. */
  148. /* This is quite a hack! */
  149. #define max_malloc_probes 20
  150. #define malloc_probe_size 64000
  151. private long
  152. heap_available(void)
  153. {    long avail = 0;
  154.     void *probes[max_malloc_probes];
  155.     uint n;
  156.     for ( n = 0; n < max_malloc_probes; n++ )
  157.       { if ( (probes[n] = malloc(malloc_probe_size)) == 0 )
  158.           break;
  159.         if_debug2('a', "[a]heap_available probe[%d]=0x%lx\n",
  160.               n, (ulong)probes[n]);
  161.         avail += malloc_probe_size;
  162.       }
  163.     while ( n )
  164.       free(probes[--n]);
  165.     return avail;
  166. }
  167.  
  168. /* Allocate various kinds of blocks. */
  169. private byte *
  170. gs_heap_alloc_bytes(gs_memory_t *mem, uint size, client_name_t cname)
  171. {    byte *ptr = 0;
  172. #ifdef DEBUG
  173.     const char *msg;
  174.     static const char *ok_msg = "OK";
  175. #  define set_msg(str) (msg = (str))
  176. #else
  177. #  define set_msg(str) DO_NOTHING
  178. #endif
  179.  
  180.     if ( size > gs_malloc_limit - sizeof(malloc_block)
  181.        )
  182.       { /* Definitely too large to allocate; also avoids overflow. */
  183.         set_msg("exceeded limit");
  184.       }
  185.     else
  186.       { uint added = size + sizeof(malloc_block);
  187.  
  188.         if ( gs_malloc_limit - added < malloc_used )
  189.           set_msg("exceeded limit");
  190.         else if ( (ptr = (byte *)malloc(added)) == 0 )
  191.           set_msg("failed");
  192.         else
  193.           { malloc_block *bp = (malloc_block *)ptr;
  194.  
  195.             if ( malloc_list )
  196.           malloc_list->prev = bp;
  197.         bp->next = malloc_list;
  198.         bp->prev = 0;
  199.         bp->size = size;
  200.         bp->type = &st_bytes;
  201.         bp->cname = cname;
  202.         malloc_list = bp;
  203.         set_msg(ok_msg);
  204.         ptr = (byte *)(bp + 1);
  205.         gs_alloc_fill(ptr, gs_alloc_fill_alloc, size);
  206.         malloc_used += size + sizeof(malloc_block);
  207.         if ( malloc_used > gs_malloc_max )
  208.           gs_malloc_max = malloc_used;
  209.           }
  210.       }
  211. #ifdef DEBUG
  212.     if ( gs_debug_c('a') || msg != ok_msg )
  213.       dprintf4("[a+]gs_malloc(%s)(%u) = 0x%lx: %s\n",
  214.            client_name_string(cname), size, (ulong)ptr, msg);
  215. #endif
  216.     return ptr;
  217. #undef set_msg
  218. }
  219. private void *
  220. gs_heap_alloc_struct(gs_memory_t *mem, gs_memory_type_ptr_t pstype,
  221.   client_name_t cname)
  222. {    void *ptr = gs_heap_alloc_bytes(mem, gs_struct_type_size(pstype), cname);
  223.     if ( ptr == 0 )
  224.       return 0;
  225.     ((malloc_block *)ptr)[-1].type = pstype;
  226.     return ptr;
  227. }
  228. private byte *
  229. gs_heap_alloc_byte_array(gs_memory_t *mem, uint num_elements, uint elt_size,
  230.   client_name_t cname)
  231. {    ulong lsize = (ulong)num_elements * elt_size;
  232.     if ( lsize != (uint)lsize )
  233.       return 0;
  234.     return gs_heap_alloc_bytes(mem, (uint)lsize, cname);
  235. }
  236. private void *
  237. gs_heap_alloc_struct_array(gs_memory_t *mem, uint num_elements,
  238.   gs_memory_type_ptr_t pstype, client_name_t cname)
  239. {    void *ptr = gs_heap_alloc_byte_array(mem, num_elements, gs_struct_type_size(pstype), cname);
  240.     if ( ptr == 0 )
  241.       return 0;
  242.     ((malloc_block *)ptr)[-1].type = pstype;
  243.     return ptr;
  244. }
  245. private void *
  246. gs_heap_resize_object(gs_memory_t *mem, void *obj, uint new_num_elements,
  247.   client_name_t cname)
  248. {    malloc_block *ptr = (malloc_block *)obj - 1;
  249.     gs_memory_type_ptr_t pstype = ptr->type;
  250.     uint old_size = gs_object_size(mem, obj) + sizeof(malloc_block);
  251.     uint new_size =
  252.       gs_struct_type_size(pstype) * new_num_elements +
  253.         sizeof(malloc_block);
  254.     malloc_block *new_ptr =
  255.       (malloc_block *)gs_realloc(ptr, old_size, new_size);
  256.  
  257.     if ( new_ptr == 0 )
  258.       return 0;
  259.     if ( new_ptr->prev )
  260.       new_ptr->prev->next = new_ptr;
  261.     else
  262.       malloc_list = new_ptr;
  263.     if ( new_ptr->next )
  264.       new_ptr->next->prev = new_ptr;
  265.     new_ptr->size = new_size - sizeof(malloc_block);
  266.     malloc_used -= old_size;
  267.     malloc_used += new_size;
  268.     if ( new_size > old_size )
  269.       gs_alloc_fill((byte *)new_ptr + old_size,
  270.             gs_alloc_fill_alloc, new_size - old_size);
  271.     return new_ptr + 1;
  272. }
  273. private uint
  274. gs_heap_object_size(gs_memory_t *mem, const void *ptr)
  275. {    return ((const malloc_block *)ptr)[-1].size;
  276. }
  277. private gs_memory_type_ptr_t
  278. gs_heap_object_type(gs_memory_t *mem, const void *ptr)
  279. {    return ((const malloc_block *)ptr)[-1].type;
  280. }
  281. private void
  282. gs_heap_free_object(gs_memory_t *mem, void *ptr, client_name_t cname)
  283. {    malloc_block *bp = malloc_list;
  284.     if ( gs_debug_c('a') )
  285.       dprintf3("[a-]gs_free(%s) 0x%lx(%u)\n",
  286.            client_name_string(cname), (ulong)ptr,
  287.            (ptr == 0 ? 0 : ((malloc_block *)ptr)[-1].size));
  288.     if ( ptr == 0 )
  289.       return;
  290.     if ( ptr == bp + 1 )
  291.       { malloc_list = bp->next;
  292.         malloc_used -= bp->size + sizeof(malloc_block);
  293.         if ( malloc_list )
  294.           malloc_list->prev = 0;
  295.         gs_alloc_fill(bp, gs_alloc_fill_free,
  296.               bp->size + sizeof(malloc_block));
  297.         free(bp);
  298.       }
  299.     else
  300.       { malloc_block *np;
  301.         for ( ; (np = bp->next) != 0; bp = np )
  302.           { if ( ptr == np + 1 )
  303.           { bp->next = np->next;
  304.             if ( np->next )
  305.               np->next->prev = bp;
  306.             malloc_used -= np->size + sizeof(malloc_block);
  307.             gs_alloc_fill(np, gs_alloc_fill_free,
  308.                   np->size + sizeof(malloc_block));
  309.             free(np);
  310.             return;
  311.           }
  312.           }
  313.         lprintf2("%s: free 0x%lx not found!\n",
  314.              client_name_string(cname), (ulong)ptr);
  315.         free((char *)((malloc_block *)ptr - 1));
  316.       }
  317. }
  318. private byte *
  319. gs_heap_alloc_string(gs_memory_t *mem, uint nbytes, client_name_t cname)
  320. {    return gs_heap_alloc_bytes(mem, nbytes, cname);
  321. }
  322. private byte *
  323. gs_heap_resize_string(gs_memory_t *mem, byte *data, uint old_num, uint new_num,
  324.   client_name_t cname)
  325. {    if ( gs_heap_object_type(mem, data) != &st_bytes )
  326.       { lprintf2("%s: resizing non-string 0x%lx!\n",
  327.              client_name_string(cname), (ulong)data);
  328.       }
  329.     return gs_heap_resize_object(mem, data, new_num, cname);
  330. }
  331. private void
  332. gs_heap_free_string(gs_memory_t *mem, byte *data, uint nbytes,
  333.   client_name_t cname)
  334. {    /****** SHOULD CHECK SIZE IF DEBUGGING ******/
  335.     gs_heap_free_object(mem, data, cname);
  336. }
  337. private void
  338. gs_heap_register_root(gs_memory_t *mem, gs_gc_root_t *rp, gs_ptr_type_t ptype,
  339.   void **up, client_name_t cname)
  340. {
  341. }
  342. private void
  343. gs_heap_unregister_root(gs_memory_t *mem, gs_gc_root_t *rp,
  344.   client_name_t cname)
  345. {
  346. }
  347. private void
  348. gs_heap_status(gs_memory_t *mem, gs_memory_status_t *pstat)
  349. {    pstat->allocated = malloc_used + heap_available();
  350.     pstat->used = malloc_used;
  351. }
  352. private void
  353. gs_heap_enable_free(gs_memory_t *mem, bool enable)
  354. {    if ( enable )
  355.       mem->procs.free_object = gs_heap_free_object,
  356.       mem->procs.free_string = gs_heap_free_string;
  357.     else
  358.       mem->procs.free_object = gs_ignore_free_object,
  359.       mem->procs.free_string = gs_ignore_free_string;
  360. }
  361.  
  362. /* Release all malloc'ed blocks. */
  363. void
  364. gs_malloc_release(void)
  365. {    malloc_block *bp = malloc_list;
  366.     malloc_block *np;
  367.     for ( ; bp != 0; bp = np )
  368.        {    np = bp->next;
  369.         if ( gs_debug_c('a') )
  370.           dprintf3("[a]gs_malloc_release(%s) 0x%lx(%u)\n",
  371.                client_name_string(bp->cname), (ulong)(bp + 1),
  372.                bp->size);
  373.         gs_alloc_fill(bp + 1, gs_alloc_fill_free, bp->size);
  374.         free(bp);
  375.        }
  376.     malloc_list = 0;
  377.     malloc_used = 0;
  378. }
  379.  
  380. /* ------ Other memory management ------ */
  381.  
  382. /* No-op freeing procedures */
  383. void
  384. gs_ignore_free_object(gs_memory_t *mem, void *data, client_name_t cname)
  385. {
  386. }
  387. void
  388. gs_ignore_free_string(gs_memory_t *mem, byte *data, uint nbytes,
  389.   client_name_t cname)
  390. {
  391. }
  392.  
  393. /* No-op pointer enumeration procedure */
  394. ENUM_PTRS_BEGIN_PROC(gs_no_struct_enum_ptrs) {
  395.   return 0;
  396. ENUM_PTRS_END_PROC }
  397.  
  398. /* No-op pointer relocation procedure */
  399. RELOC_PTRS_BEGIN(gs_no_struct_reloc_ptrs) {
  400. } RELOC_PTRS_END
  401.  
  402. /* Get the size of a structure from the descriptor. */
  403. uint
  404. gs_struct_type_size(gs_memory_type_ptr_t pstype)
  405. {    return pstype->ssize;
  406. }
  407.  
  408. /* Get the name of a structure from the descriptor. */
  409. struct_name_t
  410. gs_struct_type_name(gs_memory_type_ptr_t pstype)
  411. {    return pstype->sname;
  412. }
  413.  
  414. /* Normal freeing routine for reference-counted structures. */
  415. void
  416. rc_free_struct_only(gs_memory_t *mem, void *data, client_name_t cname)
  417. {    gs_free_object(mem, data, cname);
  418. }
  419.