home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / lib / mathlib / libconv / TRY / myalloc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  2.2 KB  |  105 lines

  1. #include <stdio.h>
  2. #include <limits.h>
  3.  
  4. #define ALIGN    sizeof(double)
  5.  
  6. char *my_malloc( int size){
  7.     char *ptr;
  8.     double *dpt;
  9.     float *fpt;
  10.     int  *ipt;
  11.     
  12.     size = ( (size+ALIGN-1) / ALIGN) * ALIGN;
  13.     size = (size+4*sizeof(double)+sizeof(int)+sizeof(float));
  14.  
  15.     ptr = (char *)malloc(size);
  16. /*
  17. * front padding
  18. */
  19.     dpt = (double *)ptr;
  20.     *dpt = DBL_MAX;
  21. /*
  22. * save the array_size
  23. */
  24.     ipt = (int *)(ptr + sizeof(double));
  25.     *ipt = size;
  26. /*
  27. * head padding
  28. */
  29.     fpt = (float *)(ptr + sizeof(double)+sizeof(int));
  30.     *fpt = FLT_MAX;
  31.     dpt = (double *)(ptr + sizeof(double)+sizeof(int)+sizeof(float));
  32.     *dpt = DBL_MIN;
  33.     dpt ++;
  34.     *dpt = DBL_MAX;
  35. /*
  36. * tail padding
  37. */
  38.     dpt = (double *)(ptr +size -sizeof(double));
  39.     *dpt = DBL_MAX;
  40. /*
  41. * returned pointer
  42. */
  43.     ptr = (char *)(ptr + 3*sizeof(double)+sizeof(int)+sizeof(float));
  44.     return(ptr);
  45. }
  46.  
  47. int my_free( char *ptr){
  48.     int this_size, nerr, i;
  49.     double *dpt;
  50.     float *fpt;
  51.     int  *ipt;
  52.     char *this;
  53.     
  54.     nerr =0;
  55.     this = (char *)(ptr -(3*sizeof(double)+sizeof(int)+sizeof(float)));
  56.  
  57. /*
  58. * CHECKING front padding
  59. */
  60.     dpt = (double *)(this);
  61.     if( (*dpt) != DBL_MAX ) {
  62.         fprintf( stderr, "Error: overwrote FRONT of freed array\n");
  63.         nerr++;
  64.     }
  65. /*
  66. * CHECKING size allocated
  67. */
  68.     ipt = (int *)(this + sizeof(double));
  69.     if( (this_size = *ipt) <= 0 ) {
  70.         fprintf( stderr, "Error: array size is negative\n");
  71.         nerr++;
  72.     }
  73. /*
  74. * CHECKING head padding
  75. */
  76.     fpt = (float *)(this + sizeof(double) + sizeof(int));
  77.     if( ((*fpt) != (float)FLT_MAX) ) {
  78.         fprintf( stderr, "Error: overwrote FLT_HEAD of freed array\n");
  79.         nerr++;
  80.     }
  81.     dpt = (double *)(this + sizeof(double) + sizeof(int) + sizeof(float));
  82.     if( ((*dpt) != DBL_MIN ) ) {
  83.         fprintf( stderr, "Error: overwrote DBL_MIN of freed array\n");
  84.         nerr++;
  85.     }
  86.     dpt ++;
  87.     if( ((*dpt) != DBL_MAX ) ) {
  88.         fprintf( stderr, "Error: overwrote DBL_HEAD of freed array\n");
  89.         nerr++;
  90.     }
  91. /*
  92. * CHECKING tail padding
  93. */
  94.     dpt=(double *)(this + this_size -sizeof(double));
  95.     if( (*dpt) != DBL_MAX ) {
  96.         fprintf( stderr, "Error: overwrote TAIL of freed array\n");
  97.         nerr++;
  98.     }
  99.     if( nerr) {
  100.     exit(-1);
  101.     }
  102.     free(this);
  103.     return(0);
  104. }
  105.