home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-387-Vol-3of3.iso / g / gs252src.zip / GS252 / GSMISC.C < prev    next >
C/C++ Source or Header  |  1992-09-17  |  11KB  |  374 lines

  1. /* Copyright (C) 1989, 1992 Aladdin Enterprises.  All rights reserved.
  2.    Distributed by Free Software Foundation, Inc.
  3.  
  4. This file is part of Ghostscript.
  5.  
  6. Ghostscript is distributed in the hope that it will be useful, but
  7. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  8. to anyone for the consequences of using it or for whether it serves any
  9. particular purpose or works at all, unless he says so in writing.  Refer
  10. to the Ghostscript General Public License for full details.
  11.  
  12. Everyone is granted permission to copy, modify and redistribute
  13. Ghostscript, but only under the conditions described in the Ghostscript
  14. General Public License.  A copy of this license is supposed to have been
  15. given to you along with Ghostscript so you can know your rights and
  16. responsibilities.  It should be in a file named COPYING.  Among other
  17. things, the copyright notice and this notice must be preserved on all
  18. copies.  */
  19.  
  20. /* gsmisc.c */
  21. /* Miscellaneous utilities for Ghostscript library */
  22. #include <errno.h>
  23. #include "gx.h"
  24. #include "malloc_.h"
  25. #include "memory_.h"
  26.  
  27. /* Define private replacements for stdin, stdout, and stderr. */
  28. FILE *gs_stdin, *gs_stdout, *gs_stderr;
  29. /* Ghostscript writes debugging output to gs_debug_out. */
  30. /* We define gs_debug and gs_debug_out even if DEBUG isn't defined, */
  31. /* so that we can compile individual modules with DEBUG set. */
  32. char gs_debug[128];
  33. FILE *gs_debug_out;
  34. /* We define gs_log_errors here for the same reason. */
  35. int gs_log_errors = 0;
  36.  
  37. /* We can turn on allocator debugging even if DEBUG isn't defined. */
  38. int gs_alloc_debug = 0;
  39. byte gs_alloc_fill_alloc = 0xa1;
  40. byte gs_alloc_fill_free = 0xf1;
  41.  
  42. /* Generate a block of unique IDs. */
  43. static ulong gs_next_id = 0;
  44. ulong
  45. gs_next_ids(uint count)
  46. {    ulong id;
  47.     if ( gs_next_id == 0 ) gs_next_id++;
  48.     id = gs_next_id;
  49.     gs_next_id += count;
  50.     return id;
  51. }
  52.  
  53. /* Versions of malloc and free compatible with Ghostscript's */
  54. /* model of memory management.  We keep track of all allocated */
  55. /* blocks so we can free them at cleanup time. */
  56. /* We must make sure that malloc_blocks leave the block aligned. */
  57. typedef struct malloc_block_s malloc_block;
  58. #define malloc_block_data\
  59.     malloc_block *next;\
  60.     uint size;\
  61.     const char *cname
  62. struct malloc_block_data_s { malloc_block_data; };
  63. struct malloc_block_s {
  64.     malloc_block_data;
  65.     byte _pad[-sizeof(struct malloc_block_data_s) & 7];    /* pad to double */
  66. };
  67. private malloc_block *malloc_list = 0;
  68. char *
  69. gs_malloc(uint num_elts, uint elt_size, const char *client_name)
  70. {    char *ptr;
  71.     const char *msg = "";
  72.     if ( num_elts > (max_uint - sizeof(malloc_block)) / elt_size )
  73.        {    /* Can't represent the size in a uint! */
  74.         msg = "too large for size_t";
  75.         ptr = 0;
  76.        }
  77.     else
  78.        {    uint size = num_elts * elt_size;
  79.         ptr = malloc(size + sizeof(malloc_block));
  80.         if ( ptr == 0 )
  81.             msg = "failed";
  82.         else
  83.            {    malloc_block *bp = (malloc_block *)ptr;
  84.             bp->next = malloc_list;
  85.             bp->size = size;
  86.             bp->cname = client_name;
  87.             malloc_list = bp;
  88.             msg = "OK";
  89.             ptr = (char *)(bp + 1);
  90.             if ( gs_alloc_debug )
  91.               { /* Clear the block in an attempt to track down */
  92.                 /* uninitialized data errors. */
  93.                 memset(ptr, gs_alloc_fill_alloc, size);
  94.               }
  95.            }
  96.        }
  97.     if ( gs_alloc_debug || !*msg )
  98.         dprintf5("gs_malloc(%s)(%u, %u) = 0x%lx: %s\n", client_name,
  99.              num_elts, elt_size, (ulong)ptr, msg);
  100.     return ptr;
  101. }
  102. void
  103. gs_free(char *ptr, uint num_elts, uint elt_size, const char *client_name)
  104. {    malloc_block *bp = malloc_list;
  105.     if ( gs_alloc_debug )
  106.         dprintf4("gs_free(%s)(0x%lx, %u, %u)\n", client_name,
  107.              (ulong)ptr, num_elts, elt_size);
  108.     if ( ptr == (char *)(bp + 1) )
  109.       {
  110. #ifdef DEBUG
  111.         if ( bp->size != num_elts * elt_size )
  112.           lprintf5("%s: free 0x%lx(%u,%u) size ~= %u\n",
  113.                client_name, (ulong)ptr, num_elts, elt_size,
  114.                bp->size);
  115. #endif
  116.         malloc_list = bp->next;
  117.         if ( gs_alloc_debug )
  118.           memset((char *)(bp + 1), gs_alloc_fill_free, bp->size);
  119.         free(bp);
  120.       }
  121.     else
  122.       { malloc_block *np;
  123.         for ( ; (np = bp->next) != 0; bp = np )
  124.           { if ( ptr == (char *)(np + 1) )
  125.           {
  126. #ifdef DEBUG
  127.             if ( np->size != num_elts * elt_size )
  128.               lprintf5("%s: free 0x%lx(%u,%u) size ~= %u\n",
  129.                    client_name, (ulong)ptr, num_elts, elt_size,
  130.                    np->size);
  131. #endif
  132.             bp->next = np->next;
  133.             if ( gs_alloc_debug )
  134.               memset((char *)(np + 1), gs_alloc_fill_free, np->size);
  135.             free(np);
  136.             return;
  137.           }
  138.           }
  139.         lprintf4("%s: free 0x%lx(%u,%u) not found\n",
  140.              client_name, (ulong)ptr, num_elts, elt_size);
  141.         free((char *)((malloc_block *)ptr - 1));
  142.       }
  143. }
  144. void
  145. gs_malloc_release()
  146. {    malloc_block *bp = malloc_list;
  147.     malloc_block *np;
  148.     for ( ; bp != 0; bp = np )
  149.        {    np = bp->next;
  150.         if ( gs_alloc_debug )
  151.           memset((char *)(bp + 1), gs_alloc_fill_free, bp->size);
  152.         free(bp);
  153.        }
  154.     malloc_list = 0;
  155. }
  156.  
  157. /* Swap bytes in 16-bit words. */
  158. /* This routine may be supplanted by assembly code. */
  159. /* src and dest must be 16-bit aligned; count must be even. */
  160. #if !USE_ASM
  161. #undef memswab2                /* see memory_.h */
  162. void
  163. memswab2(const byte *src, byte *dest, int count)
  164. {    register const uint *sptr = (const uint *)src;
  165.     register uint *dptr = (uint *)dest;
  166.     register int x;
  167.     register uint w;
  168.     for ( x = count >> 3; --x >= 0; )
  169.        {    w = sptr[0];
  170. #if arch_ints_are_short
  171.         dptr[0] = (w >> 8) + (w << 8);
  172.         w = sptr[1];
  173.         dptr[1] = (w >> 8) + (w << 8);
  174.         w = sptr[2];
  175.         dptr[2] = (w >> 8) + (w << 8);
  176.         w = sptr[3];
  177.         dptr[3] = (w >> 8) + (w << 8);
  178.         sptr += 4, dptr += 4;
  179. #else
  180.         dptr[0] = ((w << 8) & 0xff00ff00) + ((w >> 8) & 0x00ff00ff);
  181.         w = sptr[1];
  182.         dptr[1] = ((w << 8) & 0xff00ff00) + ((w >> 8) & 0x00ff00ff);
  183.         sptr += 2, dptr += 2;
  184. #endif
  185.        }
  186.     switch ( count & 6 )
  187.        {
  188.     case 6:
  189.         w = ((const ushort *)sptr)[2];
  190.         ((ushort *)dptr)[2] = (w >> 8) + (w << 8);
  191.     case 4:
  192.         w = ((const ushort *)sptr)[1];
  193.         ((ushort *)dptr)[1] = (w >> 8) + (w << 8);
  194.     case 2:
  195.         w = ((const ushort *)sptr)[0];
  196.         ((ushort *)dptr)[0] = (w >> 8) + (w << 8);
  197.     case 0:
  198.         ;
  199.        }
  200. }
  201. #endif                    /* !USE_ASM */
  202.  
  203. /* Swap bytes in 32-bit words. */
  204. /* src and dest must be 32-bit aligned; count must be 0 mod 4. */
  205. /* This routine may be supplanted by assembly code. */
  206. #if !USE_ASM
  207. #undef memswab4                /* see memory_.h */
  208. void
  209. memswab4(const byte *src, byte *dest, int count)
  210. {    register const ulong *sptr = (const ulong *)src;
  211.     register ulong *dptr = (ulong *)dest;
  212.     register int x;
  213.     register uint w0, w1;
  214.     for ( x = count >> 3; --x >= 0; )
  215.        {    w0 = sptr[0], w1 = sptr[1];
  216. #if arch_ints_are_short
  217.         dptr[0] = (w1 >> 8) + (w1 << 8);
  218.         dptr[1] = (w0 >> 8) + (w0 << 8);
  219.         w0 = sptr[2], w1 = sptr[3];
  220.         dptr[2] = (w1 >> 8) + (w1 << 8);
  221.         dptr[3] = (w0 >> 8) + (w0 << 8);
  222.         sptr += 4, dptr += 4;
  223. #else
  224.         dptr[0] =
  225.             (w0 << 24) + ((w0 & 0xff00) << 8) +
  226.             ((w0 >> 8) & 0xff00) + (w0 >> 24);
  227.         dptr[1] =
  228.             (w1 << 24) + ((w1 & 0xff00) << 8) +
  229.             ((w1 >> 8) & 0xff00) + (w1 >> 24);
  230.         sptr += 2, dptr += 2;
  231. #endif
  232.        }
  233.     if ( count & 4 )
  234.     {    w0 = ((const ushort *)sptr)[0];
  235.         w1 = ((const ushort *)sptr)[1];
  236.         ((ushort *)dptr)[0] = (w1 >> 8) + (w1 << 8);
  237.         ((ushort *)dptr)[1] = (w0 >> 8) + (w0 << 8);
  238.     }
  239. }
  240. #endif                    /* !USE_ASM */
  241.  
  242. /* Transpose an 8 x 8 block of bits.  line_size is the raster of */
  243. /* the input data.  dist is the distance between output bytes. */
  244. /* This routine may be supplanted by assembly code. */
  245. #if !USE_ASM
  246.  
  247. #if 1        /* This is the better of the two algorithms. */
  248.  
  249. void
  250. memflip8x8(const byte *inp, int line_size, byte *outp, int dist)
  251. {    register uint ae, bf, cg, dh;
  252.        {    const byte *ptr4 = inp + (line_size << 2);
  253.         ae = ((uint)*inp << 8) + *ptr4;
  254.         inp += line_size, ptr4 += line_size;
  255.         bf = ((uint)*inp << 8) + *ptr4;
  256.         inp += line_size, ptr4 += line_size;
  257.         cg = ((uint)*inp << 8) + *ptr4;
  258.         inp += line_size, ptr4 += line_size;
  259.         dh = ((uint)*inp << 8) + *ptr4;
  260.        }
  261.  
  262.     /* Check for all 8 bytes being the same. */
  263.     /* This is especially worth doing for the case where all are zero. */
  264.     if ( ae == bf && ae == cg && ae == dh && (ae >> 8) == (ae & 0xff) )
  265.        {    if ( ae == 0 ) goto store;
  266.         *outp = -((ae >> 7) & 1);
  267.         outp += dist;
  268.         *outp = -((ae >> 6) & 1);
  269.         outp += dist;
  270.         *outp = -((ae >> 5) & 1);
  271.         outp += dist;
  272.         *outp = -((ae >> 4) & 1);
  273.         outp += dist;
  274.         *outp = -((ae >> 3) & 1);
  275.         outp += dist;
  276.         *outp = -((ae >> 2) & 1);
  277.         outp += dist;
  278.         *outp = -((ae >> 1) & 1);
  279.         outp += dist;
  280.         *outp = -(ae & 1);
  281.         return;
  282.        }
  283.  
  284.        {    register uint temp;
  285.  
  286. /* Transpose a block of bits between registers. */
  287. #define transpose(r,s,mask,shift)\
  288.   r ^= (temp = ((s >> shift) ^ r) & mask);\
  289.   s ^= temp << shift
  290.  
  291. /* Transpose blocks of 4 x 4 */
  292. #define transpose4(r) transpose(r,r,0x00f0,4)
  293.     transpose4(ae);
  294.     transpose4(bf);
  295.     transpose4(cg);
  296.     transpose4(dh);
  297.  
  298. /* Transpose blocks of 2 x 2 */
  299.     transpose(ae, cg, 0x3333, 2);
  300.     transpose(bf, dh, 0x3333, 2);
  301.  
  302. /* Transpose blocks of 1 x 1 */
  303.     transpose(ae, bf, 0x5555, 1);
  304.     transpose(cg, dh, 0x5555, 1);
  305.  
  306.        }
  307.  
  308. store:    *outp = ae >> 8;
  309.     outp += dist;
  310.     *outp = bf >> 8;
  311.     outp += dist;
  312.     *outp = cg >> 8;
  313.     outp += dist;
  314.     *outp = dh >> 8;
  315.     outp += dist;
  316.     *outp = (byte)ae;
  317.     outp += dist;
  318.     *outp = (byte)bf;
  319.     outp += dist;
  320.     *outp = (byte)cg;
  321.     outp += dist;
  322.     *outp = (byte)dh;
  323. }
  324.  
  325. #else        /* This looked like a good idea, but it's no faster. */
  326.  
  327. /* Transpose an 8 x 8 block of bits.  line_size is the raster of */
  328. /* the input data.  dist is the distance between output bytes. */
  329. void
  330. memflip8x8(const byte *inp, int line_size, byte *outp, int dist)
  331. {    /* Define a table that spreads the bits of its index as follows: */
  332.     /* 0->0-3, 1->8-11, 2->16-19, 3->24-27, */
  333.     /* 4->4-7, 5->12-15, 6->20-23, 7->28-31. */
  334. #define b4(v) v,v+0xf,v+0xf00,v+0xf0f
  335. #define b8(v) b4(v),b4(v+0xf0000)
  336. #define b16(v) b8(v),b8(v+0xf000000)
  337.     static const ulong spread[256] =
  338.      { b16(0), b16(0xf0), b16(0xf000), b16(0xf0f0),
  339.        b16(0xf00000), b16(0xf000f0), b16(0xf0f000), b16(0xf0f0f0),
  340.        b16(0xf0000000), b16(0xf00000f0), b16(0xf000f000), b16(0xf000f0f0),
  341.        b16(0xf0f00000), b16(0xf0f000f0), b16(0xf0f0f000), b16(0xf0f0f0f0)
  342.      };
  343.     register ulong hi, lo, temp;
  344.     hi = spread[*inp] & 0x88888888;
  345.     inp += line_size;
  346.     hi |= spread[*inp] & 0x44444444;
  347.     inp += line_size;
  348.     hi |= spread[*inp] & 0x22222222;
  349.     inp += line_size;
  350.     hi |= spread[*inp] & 0x11111111;
  351.     inp += line_size;
  352.     lo = spread[*inp] & 0x88888888;
  353.     inp += line_size;
  354.     lo |= spread[*inp] & 0x44444444;
  355.     inp += line_size;
  356.     lo |= spread[*inp] & 0x22222222;
  357.     inp += line_size;
  358.     lo |= spread[*inp] & 0x11111111;
  359.     temp = (hi & 0xf0f0f0f0) | ((lo >> 4) & 0x0f0f0f0f);
  360.     *outp = (byte)((uint)(temp >> 16) >> 8); outp += dist;
  361.     *outp = (byte)(temp >> 16); outp += dist;
  362.     *outp = (byte)((uint)temp >> 8); outp += dist;
  363.     *outp = (byte)(temp); outp += dist;
  364.     temp = ((hi << 4) & 0xf0f0f0f0) | (lo & 0x0f0f0f0f);
  365.     *outp = (byte)((uint)(temp >> 16) >> 8); outp += dist;
  366.     *outp = (byte)(temp >> 16); outp += dist;
  367.     *outp = (byte)((uint)temp >> 8); outp += dist;
  368.     *outp = (byte)(temp);
  369. }
  370.  
  371. #endif        /* memflip8x8 */
  372.  
  373. #endif                    /* !USE_ASM */
  374.