aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/usb/host/s3c-otg/debug-mem.c
blob: de85853b603a7214f27b29ae9894ecad6c497161 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include "s3c-otg-hcdi-hcd.h" 
#include "debug-mem.h"

#define MAX_ALLOCS 1024

typedef void *ElemType;

static ElemType alloced[MAX_ALLOCS];
static int numalloced = 0;

#define fail(args...) ( printk(args), dump_stack() )

void debug_alloc(void *addr) {
  ElemType *freeloc = NULL;
  ElemType *c = alloced;
  int i;

    if(!addr)
      fail("MEMD alloc of NULL");

  for(i = 0; i < numalloced; i++, c++) {
    if(*c == NULL && freeloc == NULL)
      freeloc = c;
    else if(*c == addr)
      fail("MEMD multiple allocs of %p", addr);
  }

  if(freeloc)
    *freeloc = addr;
  else {
    if(numalloced >= MAX_ALLOCS)
      fail("MEMD too many allocs");
    else {
      alloced[numalloced++] = addr;
    }
  }
}

void debug_free(void *addr) {
  ElemType *c = alloced;
  int i;

    if(!addr)
      fail("free of NULL");

  for(i = 0; i < numalloced; i++, c++) {
    if(*c == addr) {
      *c = NULL;
      return;
    }
  }

  fail("MEMD freed addr %p was never alloced\n", addr);
}