IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 4300


Ignore:
Timestamp:
Jun 17, 2005, 12:16:29 PM (21 years ago)
Author:
eugene
Message:

added corruption tests to ohana_allocate

Location:
trunk/Ohana/src/libohana
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/Ohana/src/libohana/include/ohana_allocate.h

    r3903 r4300  
    55# ifdef OHANA_MEMORY
    66
    7 void *ohana_malloc (char *file, int line, size_t size);
    8 void *ohana_realloc (char *file, int line, void *in, size_t size);
     7void *ohana_malloc (char *file, int line, int Nelem, size_t esize);
     8void *ohana_realloc (char *file, int line, void *in, int Nelem, size_t esize);
    99void  ohana_free (char *file, int line, void *in);
    1010void  ohana_memregister_func (char *file, int line, void *ptr);
    1111void  ohana_memdump_func (int mode);
     12void  ohana_memcheck_func (int mode);
    1213
    1314# define ohana_memregister(X) ohana_memregister_func (__FILE__, __LINE__, (X));
     15# define ohana_memcheck(X) ohana_memcheck_func (X);
    1416# define ohana_memdump(X) ohana_memdump_func (X);
    1517
    16 # define ALLOCATE(X,T,S)  \
    17   X = (T *) ohana_malloc (__FILE__, __LINE__, (unsigned)(MAX(((S)*((int)sizeof(T))),1))); \
    18   if (X == NULL) { \
    19     fprintf(stderr,"failed malloc at %d in %s\n", __LINE__, __FILE__);\
    20     exit (10); }
     18# define ALLOCATE(X,T,S) \
     19  X = (T *) ohana_malloc (__FILE__, __LINE__, (S), sizeof(T))
    2120# define REALLOCATE(X,T,S) \
    22   X = (T *) ohana_realloc(__FILE__, __LINE__, X,(unsigned)(MAX(((S)*((int)sizeof(T))),1))); \
    23   if (X == NULL) { \
    24     fprintf(stderr,"failed realloc at %d in %s\n", __LINE__, __FILE__);\
    25     exit (10); }
     21  X = (T *) ohana_realloc(__FILE__, __LINE__, X, (S), sizeof(T));
    2622# define CHECK_REALLOCATE(X,T,S,N,D) \
    27   if ((N) >= (S)) { \
    28     S += D; \
    29     X = (T *) ohana_realloc(__FILE__, __LINE__, X,(unsigned)(MAX(((S)*((int)sizeof(T))),1))); \
    30     if (X == NULL) { \
    31       fprintf(stderr,"failed realloc increment at %d in %s\n", __LINE__, __FILE__);\
    32       exit (10); } }
     23  if ((N) >= (S)) { S += D; \
     24  X = (T *) ohana_realloc(__FILE__, __LINE__, X, (S), sizeof(T)); }
    3325# define free(X) ohana_free(__FILE__, __LINE__, X)
    3426
    3527# else
    3628# define ohana_memregister(X) /* NOP */
     29# define ohana_memcheck(X) /* NOP */
    3730# define ohana_memdump(X) /* NOP */
    3831# endif /* OHANA_MEMORY */
  • trunk/Ohana/src/libohana/src/ohana_allocate.c

    r3921 r4300  
    44
    55/* need an internal version that does not use ohana_memory functions */
     6# define FALSE 0
     7# define TRUE 1
    68# define MAX(X,Y) ((X) > (Y) ? (X) : (Y))
    79# define OHANA_ALLOCATE(X,T,S)  \
     
    2426# define OHANA_FREE(X) free(X)
    2527
    26 # define STATE_ALLOC   0
    27 # define STATE_REALLOC 1
    28 # define STATE_FREE    2
     28# define STATE_ALLOC    0
     29# define STATE_REALLOC  1
     30# define STATE_FREE     2
     31# define STATE_EXTERNAL 3
    2932
    3033typedef struct {
    3134  char *file;
    3235  int   line;
    33   void *ptr;
     36  void *ptr;   // location of externally visible entry
     37  int   size;
    3438  int   state;
    3539} Memlist;
     
    5458}
    5559
    56 void *ohana_malloc (char *file, int line, size_t size) {
    57 
    58   void *ptr;
     60void *ohana_malloc (char *file, int line, int Nelem, size_t esize) {
     61
     62  void *ptr, *new;
     63  int size;
     64  int *marker;
    5965
    6066  if (memlist == NULL) ohana_meminit ();
    6167
    62   ptr = malloc (size);
    63   if (ptr == NULL) ohana_memabort ("failed to allocate memory (%s, %d)\n", file, line);
     68  Nelem = MAX (1, Nelem);
     69  size = Nelem * esize;
     70
     71  new = malloc (size + 8); /* 8 extra to save endposts */
     72  if (new == NULL) ohana_memabort ("failed to allocate memory (%s, %d)\n", file, line);
     73  ptr = new + 4;
     74
     75  marker = (int *) new;
     76  *marker = 0xdeadbeef;
     77  marker = (int *)(new + size + 4);
     78  *marker = 0xdeadbeef;
    6479
    6580  /* new memory, add to stack */
     
    6782  memlist[Nmemlist].file = file;
    6883  memlist[Nmemlist].line = line;
     84  memlist[Nmemlist].size = size;
    6985  memlist[Nmemlist].state = STATE_ALLOC;
    7086  Nmemlist ++;
     
    7692}
    7793
    78 /* register externally allocated memory with ohana memory manager */
    79 void ohana_memregister_func (char *file, int line, void *ptr) {
    80 
    81   if (memlist == NULL) ohana_meminit ();
    82 
    83   /* add to stack */
    84   memlist[Nmemlist].ptr  = ptr;
    85   memlist[Nmemlist].file = file;
    86   memlist[Nmemlist].line = line;
    87   memlist[Nmemlist].state = STATE_ALLOC;
    88   Nmemlist ++;
    89   if (Nmemlist == NMEMLIST) {
    90     NMEMLIST += 1000;
    91     OHANA_REALLOCATE (memlist, Memlist, NMEMLIST);
    92   }
    93   return;
    94 }
    95 
    96 void *ohana_realloc (char *file, int line, void *in, size_t size) {
    97 
    98   int i;
    99   void *ptr;
     94void *ohana_realloc (char *file, int line, void *in, int Nelem, size_t esize) {
     95
     96  int i, size;
     97  void *ptr, *ref, *new;
     98  int *marker;
    10099
    101100  if (memlist == NULL) ohana_memabort ("REALLOCATE before ALLOCATE");
     101
     102  Nelem = MAX (1, Nelem);
     103  size = Nelem * esize;
     104
     105  ref = in - 4;
    102106
    103107  /* find old entry, update ptr, file, line */
     
    105109    if (memlist[i].state == STATE_FREE) continue;
    106110    if (memlist[i].ptr == in) {
     111
     112      if (memlist[i].state == STATE_EXTERNAL) ohana_memabort ("ERROR: realloc of external memory");
     113
    107114      /* ask for new memory */
    108       ptr = realloc (in, size);
    109       if (ptr == NULL) ohana_memabort ("failed to reallocate memory (%s, %d)\n", file, line);
    110       /* if new memory is same as old, just return it */
    111       if (in == ptr) return (ptr);
    112       /* otherwise, update with new location */
     115      new = realloc (ref, size + 8); /* 8 extra to save endposts */
     116      if (new == NULL) ohana_memabort ("failed to reallocate memory (%s, %d)\n", file, line);
     117      ptr = new + 4;
     118     
     119      /* set the marker */
     120      marker = (int *) new;
     121      *marker = 0xdeadbeef;
     122      marker = (int *)(ptr + size);
     123      *marker = 0xdeadbeef;
     124
     125      /* otherwise, update memory new location */
    113126      memlist[i].ptr = ptr;
    114       memlist[i].file = file;
    115       memlist[i].line = line;
     127      memlist[i].size = size;
    116128      memlist[i].state = STATE_REALLOC;
     129
     130      /* if new memory in new location, update references */
     131      if (ptr != in) {
     132        memlist[i].file = file;
     133        memlist[i].line = line;
     134      }
     135
    117136      return (ptr);
    118137    }
    119138  }
    120 
    121139  ohana_memabort ("allocated memory not found for realloc (%s, %d)\n", file, line);
    122140  return (NULL);
     
    126144
    127145  int i;
     146
    128147  if (memlist == NULL) ohana_memabort ("FREE before ALLOCATE");
    129148
     
    141160}
    142161
    143 void ohana_memdump_func (int allmemory) {
    144 
    145   int i, Ns[3], N;
    146   char S[3];
     162/* register externally allocated memory with ohana memory manager */
     163void ohana_memregister_func (char *file, int line, void *ptr) {
     164
     165  if (memlist == NULL) ohana_meminit ();
     166
     167  /* add to stack */
     168  memlist[Nmemlist].ptr  = ptr;
     169  memlist[Nmemlist].file = file;
     170  memlist[Nmemlist].line = line;
     171  memlist[Nmemlist].state = STATE_EXTERNAL;
     172  Nmemlist ++;
     173  if (Nmemlist == NMEMLIST) {
     174    NMEMLIST += 1000;
     175    OHANA_REALLOCATE (memlist, Memlist, NMEMLIST);
     176  }
     177  return;
     178}
     179
     180void ohana_memcheck_func (int allmemory) {
     181
     182  int i, header;
     183  int *marker;
     184  char top, bottom;
    147185
    148186  if (Nmemlist == 0) {
     
    151189  }
    152190
     191  fprintf (stderr, "checking for memory corruption...\n");
     192  header = FALSE;
     193  for (i = 0; i < Nmemlist; i++) {
     194    if (memlist[i].state == STATE_EXTERNAL) continue;
     195    if ((allmemory == 0) && (memlist[i].state == STATE_FREE)) continue;
     196
     197    top = bottom = 'Y';
     198    marker = (int *)(memlist[i].ptr - 4);
     199    if (*marker == 0xdeadbeef) bottom = 'N';
     200    marker = (int *)(memlist[i].ptr + memlist[i].size);
     201    if (*marker == 0xdeadbeef) top = 'N';
     202   
     203    if ((top == 'N') && (bottom == 'N')) continue;
     204    if (!header) {
     205      fprintf (stderr, "         Nmem bot top file            line\n");
     206      header = TRUE;
     207    }
     208    fprintf (stderr, "corrupt:  %3d  %c   %c  %-15s %3d\n", i, bottom, top, memlist[i].file, memlist[i].line);
     209  }
     210  return;
     211}
     212
     213void ohana_memdump_func (int allmemory) {
     214
     215  int i, Ns[3], N;
     216  char S[3];
     217
     218  if (Nmemlist == 0) {
     219    fprintf (stderr, "no memory allocated\n");
     220    return;
     221  }
     222
    153223  S[0] = 'A';
    154224  S[1] = 'R';
    155225  S[2] = 'F';
    156   Ns[0] = Ns[1] = Ns[2] = 0;
    157 
    158   for (i = 0; i < Nmemlist; i++) {
    159     if (memlist[i].state == STATE_ALLOC)   N = 0;
    160     if (memlist[i].state == STATE_REALLOC) N = 1;
    161     if (memlist[i].state == STATE_FREE)    N = 2;
     226  S[3] = 'X';
     227  Ns[0] = Ns[1] = Ns[2] = Ns[3] = 0;
     228
     229  for (i = 0; i < Nmemlist; i++) {
     230    if (memlist[i].state == STATE_ALLOC)    N = 0;
     231    if (memlist[i].state == STATE_REALLOC)  N = 1;
     232    if (memlist[i].state == STATE_FREE)     N = 2;
     233    if (memlist[i].state == STATE_EXTERNAL) N = 2;
    162234    Ns[N] ++;
    163235    if ((allmemory == 0) && (memlist[i].state == STATE_FREE)) continue;
Note: See TracChangeset for help on using the changeset viewer.