summaryrefslogtreecommitdiff
path: root/libclapi/cl_alloc.c
blob: 2dabd3d5d9d3f5d41856bae4100025858f08915d (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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
 * Copyright © 2012 Intel Corporation
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
 *
 */
#include <stdlib.h>
#include <assert.h>
#include <malloc.h>
#include <pthread.h>
#include <string.h>
#include "cl_alloc.h"
#include "cl_internals.h"

static pthread_mutex_t cl_alloc_log_lock;

typedef struct _cl_alloc_log_item {
  void* ptr;
  size_t size;
  char* file;
  int line;
} _cl_alloc_log_item;
typedef struct _cl_alloc_log_item*  cl_alloc_log_item;

static cl_alloc_log_item* cl_alloc_log_map[8];
static int cl_alloc_log_map_size[8];

LOCAL void cl_alloc_log_init(void)
{
  static int inited = 0;
  int i;
  if (inited)
    return;

  pthread_mutex_init(&cl_alloc_log_lock, NULL);

  for (i = 0; i < 8; i++) {
    cl_alloc_log_map_size[i] = 128;
    cl_alloc_log_map[i] = malloc(cl_alloc_log_map_size[i]*sizeof(cl_alloc_log_item));
    memset(cl_alloc_log_map[i], 0, cl_alloc_log_map_size[i]*sizeof(cl_alloc_log_item));
  }

  inited = 1;
}

static void insert_alloc_log_item(void* ptr, size_t sz, char* file, int line)
{
  cl_long slot;
  int i;

  slot = (cl_long)ptr;
  slot = (slot>>5) & 0x07;
  assert(slot < 8);

  cl_alloc_log_item it = malloc(sizeof(cl_alloc_log_item));
  assert(it);
  it->ptr = ptr;
  it->size = sz;
  it->file = file;
  it->line = line;

  pthread_mutex_lock(&cl_alloc_log_lock);
  for (i = 0; i < cl_alloc_log_map_size[slot]; i++) {
    if (cl_alloc_log_map[slot][i] == NULL) {
      break;
    }
  }

  if (i == cl_alloc_log_map_size[slot]) {
    cl_alloc_log_map[slot] =
      realloc(cl_alloc_log_map[slot], 2*cl_alloc_log_map_size[slot]*sizeof(cl_alloc_log_item));
    memset(cl_alloc_log_map[slot] + cl_alloc_log_map_size[slot], 0,
           cl_alloc_log_map_size[slot]*sizeof(cl_alloc_log_item));
    cl_alloc_log_map_size[slot] = cl_alloc_log_map_size[slot]*2;
  }

  cl_alloc_log_map[slot][i] = it;
  pthread_mutex_unlock(&cl_alloc_log_lock);
}

static void delete_alloc_log_item(void* ptr, char* file, int line)
{
  cl_long slot;
  int i;

  slot = (cl_long)ptr;
  slot = (slot>>5) & 0x07;
  assert(slot < 8);

  pthread_mutex_lock(&cl_alloc_log_lock);
  for (i = 0; i < cl_alloc_log_map_size[slot]; i++) {
    if (cl_alloc_log_map[slot][i]->ptr == ptr) {
      break;
    }
  }

  if (i == cl_alloc_log_map_size[slot]) {
    printf("Free at file: %s, line: %d, We can not find the malloc log for this ptr:%p, fatal\n",
           file, line, ptr);
    assert(0);
  }

  free(cl_alloc_log_map[slot][i]);
  cl_alloc_log_map[slot][i] = NULL;

  pthread_mutex_unlock(&cl_alloc_log_lock);
}

LOCAL void* cl_malloc(size_t sz, char* file, int line)
{
  void* p = malloc(sz);
  assert(p);
  insert_alloc_log_item(p, sz, file, line);
  return p;
}

LOCAL void*
cl_aligned_malloc(size_t sz, size_t align, char* file, int line)
{
  void * p = NULL;
  p = memalign(align, sz);
  assert(p);
  insert_alloc_log_item(p, ((sz + align - 1)/align)*align, file, line);
  return p;
}

LOCAL void*
cl_calloc(size_t n, size_t elem_size, char* file, int line)
{
  void *p = NULL;
  p = calloc(n, elem_size);
  assert(p);
  insert_alloc_log_item(p, n*elem_size, file, line);
  return p;
}

LOCAL void*
cl_realloc(void *ptr, size_t sz, char* file, int line)
{
  void *p = NULL;

  if (ptr != NULL) {
    delete_alloc_log_item(ptr, file, line);
  }

  p = realloc(ptr, sz);
  assert(p);
  insert_alloc_log_item(p, sz, file, line);
  return p;
}

LOCAL void
cl_free(void *ptr, char* file, int line)
{
  if (ptr == NULL)
    return;

  delete_alloc_log_item(ptr, file, line);
  free(ptr);
}

LOCAL void
cl_alloc_report_unfreed(void)
{
}