home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / lisp / eulisp / feel0_89.lha / Feel / AddOns / feel_malloc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-05-13  |  1.9 KB  |  83 lines

  1. /* 
  2.  * Eulisp version of malloc
  3.  * may not be safe...
  4.  */
  5. #include "defs.h"
  6. #include "structs.h"
  7. #include "allocate.h"
  8.  
  9. #define PAGBITS 5
  10. #define MAXHEAPLEN 16384
  11. #define BIGINT 0xffffffff
  12.  
  13. char *feel_malloc(int size)
  14. {
  15.   char *data;
  16.   int len;
  17.  
  18.   char *ptr;
  19.    /* keep the numbers round */
  20.  
  21.   len = (((size + 2*sizeof(int)) >> PAGBITS) << PAGBITS) + (1 << PAGBITS);
  22.  
  23. #ifdef debug /* Tue Apr 23 16:45:03 1991 */
  24. /**/  if (len < size)
  25. /**/  {
  26. /**/    fprintf(stderr,"Feel-malloc bug sz: %d len:%d \n",size,len);
  27. /**/    exit(0);
  28. /**/  }
  29. #endif /* debug Tue Apr 23 16:45:03 1991 */
  30.  
  31.   if (len < MAXHEAPLEN)
  32.     data = allocate_space(NULL,len);
  33.   else
  34.     data = allocate_stack(NULL,len);
  35.  
  36.  
  37. #ifdef notdef /* Tue Apr 23 16:45:29 1991 */
  38. /**/  fprintf(stderr,"Malloc: allocating %x bytes at %x\n",len,data+4);  
  39. #endif /* notdef Tue Apr 23 16:45:29 1991 */
  40.  
  41.   if(data == NULL)
  42.     {
  43.       fprintf(stderr,"Malloc failed...panicing. Request was for %d(%d) bytes.",size,len);
  44.       exit(0);
  45.     }
  46.  
  47.   *((int *) (data + len - sizeof(int))) = 0xdeadbeef;
  48.   *(int *) data = len;
  49.  
  50. #ifdef notdef /* Tue Apr 23 16:45:48 1991 */
  51. /**/  for (ptr = data + sizeof(int) ; ptr < data + len - sizeof(int) ; ptr ++)
  52. /**/    *ptr='x';
  53. #endif /* notdef Tue Apr 23 16:45:48 1991 */
  54.  
  55.   return (data + sizeof(int));
  56. }
  57.  
  58.  
  59. void feel_free(char *data)
  60. {
  61.   int len;
  62.  
  63. #ifdef notdef /* Tue Apr 23 16:46:23 1991 */
  64. /**/  printf("Malloc: freeing: %x bytes at %x\n",*((int *) (data-4)),data);
  65. /**/  if ( *((int *) (data + len - 2*sizeof(int))) != 0xdeadbeef)
  66. /**/    {
  67. /**/      fprintf(stderr,"DataSpace overrun: 0x%x\n",data);
  68. /*      exit(255);*/
  69. /**/    }
  70. #endif /* notdef Tue Apr 23 16:46:23 1991 */
  71.   len = *((int *) (data - sizeof(int)));
  72.   
  73.   if (len < MAXHEAPLEN) /* Null is stack pointer --- pray for no errors */
  74.     deallocate_space(NULL,data - sizeof(int), len);
  75.   else
  76.     deallocate_stack(NULL,data - sizeof(int), len);
  77.   
  78.   return;
  79. }
  80.  
  81.  
  82.  
  83.