Back to index

plt-scheme  4.2.1
Classes | Defines | Typedefs | Functions | Variables
checkm.c File Reference

Go to the source code of this file.

Classes

struct  HEADER

Defines

#define GC_too   0
#define SPACE_VAL   0x77777777
#define CHAR_SPACE_VAL   ((unsigned char)0x99)
#define FREED_VAL   0x55555555

Typedefs

typedef long SPACE
typedef struct HEADER HEADER

Functions

voidmalloc (unsigned long len)
voidrealloc (void *p, unsigned long len)
voidcalloc (unsigned long len0, unsigned long len)
void free (void *p)
unsigned long checkmem (void)
long get_total (void)

Variables

static long total_allocated
HEADERchain_start
HEADERchain_end
static long looking_for

Class Documentation

struct HEADER

Definition at line 15 of file checkm.c.

Collaboration diagram for HEADER:
Class Members
unsigned long exact_len
unsigned long len
struct HEADER * next
struct HEADER * prev
SPACE s

Define Documentation

#define CHAR_SPACE_VAL   ((unsigned char)0x99)

Definition at line 10 of file checkm.c.

#define FREED_VAL   0x55555555

Definition at line 11 of file checkm.c.

#define GC_too   0

Definition at line 6 of file checkm.c.

#define SPACE_VAL   0x77777777

Definition at line 9 of file checkm.c.


Typedef Documentation

typedef struct HEADER HEADER
typedef long SPACE

Definition at line 8 of file checkm.c.


Function Documentation

void* calloc ( unsigned long  len0,
unsigned long  len 
)

Definition at line 85 of file checkm.c.

{
  void *v;

  len *= len0;
  v = malloc(len);
  memset(v, 0, len);
  return v;
}

Here is the caller graph for this function:

unsigned long checkmem ( void  )

Definition at line 134 of file checkm.c.

{
  HEADER *h, *prev = 0;
  SPACE v;
  unsigned long total_use = 0;

  for (h = chain_start; h; prev = h, h = h->next) {
    if (h->s != SPACE_VAL)
      printf("%lx [after %lx]: bad start (%lx)\n", h, prev, h->s);

    if (h->exact_len) {
      total_use += h->exact_len;

      if (h->exact_len < h->len) {
       int i;
       for (i = h->exact_len; i < h->len; i++) {
         v = (*(unsigned char *)((long)h + sizeof(HEADER) + i));
         if (v != CHAR_SPACE_VAL)
           printf("%lx: bad inexact end (%lx)\n", h, v);
       }
      }
    } else {
      int i;
      for (i = h->len; i; ) {
       i -= sizeof(long);
       v = (*(long *)((long)h + sizeof(HEADER) + i));
       if (v != FREED_VAL)
         printf("%lx: bad freed val (%lx)\n", h, v);
      }
    }

    v = (*(SPACE *)((long)h + sizeof(HEADER) + h->len));
    if (v != SPACE_VAL)
      printf("%lx: bad end (%lx)\n", h, v);
  }

  return total_use;
}
void free ( void p)

Definition at line 95 of file checkm.c.

{
  HEADER *h = (HEADER *)(((long)p) - sizeof(HEADER));
  SPACE v;

  if (!p)
    return;

  if (!h->exact_len) {
    printf("%lx: double-free (%lx)\n", h, h->len);
  } else {
    int i;

    if (h->s != SPACE_VAL)
      printf("%lx: bad start for free (%lx)\n", h, h->s);

    if (h->exact_len < h->len) {
      for (i = h->exact_len; i < h->len; i++) {
       v = (*(unsigned char *)((long)h + sizeof(HEADER) + i));
       if (v != CHAR_SPACE_VAL)
         printf("%lx: bad inexact end for free (%lx)\n", h, v);
      }
    }

    v = (*(SPACE *)((long)h + sizeof(HEADER) + h->len));
    if (v != SPACE_VAL)
      printf("%lx: bad end for free (%lx)\n", h, v);

    for (i = h->len; i; ) {
      i -= sizeof(long);
      (*(long *)((long)h + sizeof(HEADER) + i)) = FREED_VAL;
    }
  }

  total_allocated -= h->exact_len;

  h->exact_len = 0;
}
long get_total ( void  )

Definition at line 173 of file checkm.c.

{
  return total_allocated;
}
void* malloc ( unsigned long  len)

Definition at line 27 of file checkm.c.

{
  long pos, d, exact_len = len;
  HEADER *h;

  total_allocated += exact_len;

  d = len % sizeof(long);
  if (d)
    len += (sizeof(long) - d);

  pos = sbrk(len + sizeof(HEADER) + sizeof(SPACE));

  if (pos == looking_for) {
    /* Found it ... */
    looking_for = 0;
  }

  h = (HEADER *)pos;
  if (chain_end)
    chain_end->next = h;
  else
    chain_start = h;
  h->prev = chain_end;
  h->len = len;
  h->exact_len = exact_len;

  h->s = SPACE_VAL;
  
  *(SPACE *)(pos + sizeof(HEADER) + len) = SPACE_VAL;
  while (exact_len < len) {
    *(unsigned char *)(pos + sizeof(HEADER) + exact_len) = CHAR_SPACE_VAL;
    exact_len++;
  }

  chain_end = h;

  return (void *)(pos + sizeof(HEADER));
}
void* realloc ( void p,
unsigned long  len 
)

Definition at line 67 of file checkm.c.

{
  void *n;
  unsigned long olen;

  olen = ((HEADER *)((long)p - sizeof(HEADER)))->len;
  if (olen > len)
    return p;

  n = malloc(len);

  memcpy(n, p, olen);

  free(p);

  return n;
}

Here is the caller graph for this function:


Variable Documentation

Definition at line 23 of file checkm.c.

Definition at line 23 of file checkm.c.

long looking_for [static]

Definition at line 25 of file checkm.c.

long total_allocated [static]

Definition at line 13 of file checkm.c.