summaryrefslogtreecommitdiff
path: root/vpx_mem/vpx_mem.c
blob: baf005c774bb45f6f270e53cd6bc97b0f7f6c261 (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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
/*
 *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */


#define __VPX_MEM_C__

#include "vpx_mem.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "include/vpx_mem_intrnl.h"
#include "vpx/vpx_integer.h"

#if CONFIG_MEM_TRACKER
#ifndef VPX_NO_GLOBALS
static unsigned long g_alloc_count = 0;
#else
#include "vpx_global_handling.h"
#define g_alloc_count vpxglobalm(vpxmem,g_alloc_count)
#endif
#endif

#if USE_GLOBAL_FUNCTION_POINTERS
struct GLOBAL_FUNC_POINTERS {
  g_malloc_func g_malloc;
  g_calloc_func g_calloc;
  g_realloc_func g_realloc;
  g_free_func g_free;
  g_memcpy_func g_memcpy;
  g_memset_func g_memset;
  g_memmove_func g_memmove;
} *g_func = NULL;

# define VPX_MALLOC_L  g_func->g_malloc
# define VPX_REALLOC_L g_func->g_realloc
# define VPX_FREE_L    g_func->g_free
# define VPX_MEMCPY_L  g_func->g_memcpy
# define VPX_MEMSET_L  g_func->g_memset
# define VPX_MEMMOVE_L g_func->g_memmove
#else
# define VPX_MALLOC_L  malloc
# define VPX_REALLOC_L realloc
# define VPX_FREE_L    free
# define VPX_MEMCPY_L  memcpy
# define VPX_MEMSET_L  memset
# define VPX_MEMMOVE_L memmove
#endif /* USE_GLOBAL_FUNCTION_POINTERS */

unsigned int vpx_mem_get_version() {
  unsigned int ver = ((unsigned int)(unsigned char)VPX_MEM_VERSION_CHIEF << 24 |
                      (unsigned int)(unsigned char)VPX_MEM_VERSION_MAJOR << 16 |
                      (unsigned int)(unsigned char)VPX_MEM_VERSION_MINOR << 8  |
                      (unsigned int)(unsigned char)VPX_MEM_VERSION_PATCH);
  return ver;
}

void *vpx_memalign(size_t align, size_t size) {
  void *addr,
       * x = NULL;

  addr = VPX_MALLOC_L(size + align - 1 + ADDRESS_STORAGE_SIZE);

  if (addr) {
    x = align_addr((unsigned char *)addr + ADDRESS_STORAGE_SIZE, (int)align);
    /* save the actual malloc address */
    ((size_t *)x)[-1] = (size_t)addr;
  }

  return x;
}

void *vpx_malloc(size_t size) {
  return vpx_memalign(DEFAULT_ALIGNMENT, size);
}

void *vpx_calloc(size_t num, size_t size) {
  void *x;

  x = vpx_memalign(DEFAULT_ALIGNMENT, num * size);

  if (x)
    VPX_MEMSET_L(x, 0, num * size);

  return x;
}

void *vpx_realloc(void *memblk, size_t size) {
  void *addr,
       * new_addr = NULL;
  int align = DEFAULT_ALIGNMENT;

  /*
  The realloc() function changes the size of the object pointed to by
  ptr to the size specified by size, and returns a pointer to the
  possibly moved block. The contents are unchanged up to the lesser
  of the new and old sizes. If ptr is null, realloc() behaves like
  malloc() for the specified size. If size is zero (0) and ptr is
  not a null pointer, the object pointed to is freed.
  */
  if (!memblk)
    new_addr = vpx_malloc(size);
  else if (!size)
    vpx_free(memblk);
  else {
    addr   = (void *)(((size_t *)memblk)[-1]);
    memblk = NULL;

    new_addr = VPX_REALLOC_L(addr, size + align + ADDRESS_STORAGE_SIZE);

    if (new_addr) {
      addr = new_addr;
      new_addr = (void *)(((size_t)
                           ((unsigned char *)new_addr + ADDRESS_STORAGE_SIZE) + (align - 1)) &
                          (size_t) - align);
      /* save the actual malloc address */
      ((size_t *)new_addr)[-1] = (size_t)addr;
    }
  }

  return new_addr;
}

void vpx_free(void *memblk) {
  if (memblk) {
    void *addr = (void *)(((size_t *)memblk)[-1]);
    VPX_FREE_L(addr);
  }
}

#if CONFIG_MEM_TRACKER
void *xvpx_memalign(size_t align, size_t size, char *file, int line) {
#if TRY_BOUNDS_CHECK
  unsigned char *x_bounds;
#endif

  void *x;

  if (g_alloc_count == 0) {
#if TRY_BOUNDS_CHECK
    int i_rv = vpx_memory_tracker_init(BOUNDS_CHECK_PAD_SIZE, BOUNDS_CHECK_VALUE);
#else
    int i_rv = vpx_memory_tracker_init(0, 0);
#endif

    if (i_rv < 0) {
      _P(printf("ERROR xvpx_malloc MEM_TRACK_USAGE error vpx_memory_tracker_init().\n");)
    }
  }

#if TRY_BOUNDS_CHECK
  {
    int i;
    unsigned int tempme = BOUNDS_CHECK_VALUE;

    x_bounds = vpx_memalign(align, size + (BOUNDS_CHECK_PAD_SIZE * 2));

    if (x_bounds) {
      /*we're aligning the address twice here but to keep things
        consistent we want to have the padding come before the stored
        address so no matter what free function gets called we will
        attempt to free the correct address*/
      x_bounds = (unsigned char *)(((size_t *)x_bounds)[-1]);
      x = align_addr(x_bounds + BOUNDS_CHECK_PAD_SIZE + ADDRESS_STORAGE_SIZE,
                     (int)align);
      /* save the actual malloc address */
      ((size_t *)x)[-1] = (size_t)x_bounds;

      for (i = 0; i < BOUNDS_CHECK_PAD_SIZE; i += sizeof(unsigned int)) {
        VPX_MEMCPY_L(x_bounds + i, &tempme, sizeof(unsigned int));
        VPX_MEMCPY_L((unsigned char *)x + size + i,
                     &tempme, sizeof(unsigned int));
      }
    } else
      x = NULL;
  }
#else
  x = vpx_memalign(align, size);
#endif /*TRY_BOUNDS_CHECK*/

  g_alloc_count++;

  vpx_memory_tracker_add((size_t)x, (unsigned int)size, file, line, 1);

  return x;
}

void *xvpx_malloc(size_t size, char *file, int line) {
  return xvpx_memalign(DEFAULT_ALIGNMENT, size, file, line);
}

void *xvpx_calloc(size_t num, size_t size, char *file, int line) {
  void *x = xvpx_memalign(DEFAULT_ALIGNMENT, num * size, file, line);

  if (x)
    VPX_MEMSET_L(x, 0, num * size);

  return x;
}

void *xvpx_realloc(void *memblk, size_t size, char *file, int line) {
  struct mem_block *p = NULL;
  int orig_size = 0,
      orig_line = 0;
  char *orig_file = NULL;

#if TRY_BOUNDS_CHECK
  unsigned char *x_bounds = memblk ?
                            (unsigned char *)(((size_t *)memblk)[-1]) :
                            NULL;
#endif

  void *x;

  if (g_alloc_count == 0) {
#if TRY_BOUNDS_CHECK

    if (!vpx_memory_tracker_init(BOUNDS_CHECK_PAD_SIZE, BOUNDS_CHECK_VALUE))
#else
    if (!vpx_memory_tracker_init(0, 0))
#endif
    {
      _P(printf("ERROR xvpx_malloc MEM_TRACK_USAGE error vpx_memory_tracker_init().\n");)
    }
  }

  if ((p = vpx_memory_tracker_find((size_t)memblk))) {
    orig_size = p->size;
    orig_file = p->file;
    orig_line = p->line;
  }

#if TRY_BOUNDS_CHECK_ON_FREE
  vpx_memory_tracker_check_integrity(file, line);
#endif

  /* have to do this regardless of success, because
   * the memory that does get realloc'd may change
   * the bounds values of this block
   */
  vpx_memory_tracker_remove((size_t)memblk);

#if TRY_BOUNDS_CHECK
  {
    int i;
    unsigned int tempme = BOUNDS_CHECK_VALUE;

    x_bounds = vpx_realloc(memblk, size + (BOUNDS_CHECK_PAD_SIZE * 2));

    if (x_bounds) {
      x_bounds = (unsigned char *)(((size_t *)x_bounds)[-1]);
      x = align_addr(x_bounds + BOUNDS_CHECK_PAD_SIZE + ADDRESS_STORAGE_SIZE,
                     (int)DEFAULT_ALIGNMENT);
      /* save the actual malloc address */
      ((size_t *)x)[-1] = (size_t)x_bounds;

      for (i = 0; i < BOUNDS_CHECK_PAD_SIZE; i += sizeof(unsigned int)) {
        VPX_MEMCPY_L(x_bounds + i, &tempme, sizeof(unsigned int));
        VPX_MEMCPY_L((unsigned char *)x + size + i,
                     &tempme, sizeof(unsigned int));
      }
    } else
      x = NULL;
  }
#else
  x = vpx_realloc(memblk, size);
#endif /*TRY_BOUNDS_CHECK*/

  if (!memblk) ++g_alloc_count;

  if (x)
    vpx_memory_tracker_add((size_t)x, (unsigned int)size, file, line, 1);
  else
    vpx_memory_tracker_add((size_t)memblk, orig_size, orig_file, orig_line, 1);

  return x;
}

void xvpx_free(void *p_address, char *file, int line) {
#if TRY_BOUNDS_CHECK
  unsigned char *p_bounds_address = (unsigned char *)p_address;
  /*p_bounds_address -= BOUNDS_CHECK_PAD_SIZE;*/
#endif

#if !TRY_BOUNDS_CHECK_ON_FREE
  (void)file;
  (void)line;
#endif

  if (p_address) {
#if TRY_BOUNDS_CHECK_ON_FREE
    vpx_memory_tracker_check_integrity(file, line);
#endif

    /* if the addr isn't found in the list, assume it was allocated via
     * vpx_ calls not xvpx_, therefore it does not contain any padding
     */
    if (vpx_memory_tracker_remove((size_t)p_address) == -2) {
      p_bounds_address = p_address;
      _P(fprintf(stderr, "[vpx_mem][xvpx_free] addr: %p not found in"
                 " list; freed from file:%s"
                 " line:%d\n", p_address, file, line));
    } else
      --g_alloc_count;

#if TRY_BOUNDS_CHECK
    vpx_free(p_bounds_address);
#else
    vpx_free(p_address);
#endif

    if (!g_alloc_count)
      vpx_memory_tracker_destroy();
  }
}

#endif /*CONFIG_MEM_TRACKER*/

void *vpx_memcpy(void *dest, const void *source, size_t length) {
  return VPX_MEMCPY_L(dest, source, length);
}

void *vpx_memset(void *dest, int val, size_t length) {
  return VPX_MEMSET_L(dest, val, length);
}

#if CONFIG_VP9 && CONFIG_VP9_HIGHBITDEPTH
void *vpx_memset16(void *dest, int val, size_t length) {
  int i;
  void *orig = dest;
  uint16_t *dest16 = dest;
  for (i = 0; i < length; i++)
    *dest16++ = val;
  return orig;
}
#endif  // CONFIG_VP9 && CONFIG_VP9_HIGHBITDEPTH

void *vpx_memmove(void *dest, const void *src, size_t count) {
  return VPX_MEMMOVE_L(dest, src, count);
}

#if USE_GLOBAL_FUNCTION_POINTERS
# if CONFIG_MEM_TRACKER
extern int vpx_memory_tracker_set_functions(g_malloc_func g_malloc_l
, g_calloc_func g_calloc_l
, g_realloc_func g_realloc_l
, g_free_func g_free_l
, g_memcpy_func g_memcpy_l
, g_memset_func g_memset_l
, g_memmove_func g_memmove_l);
# endif
#endif /*USE_GLOBAL_FUNCTION_POINTERS*/
int vpx_mem_set_functions(g_malloc_func g_malloc_l
, g_calloc_func g_calloc_l
, g_realloc_func g_realloc_l
, g_free_func g_free_l
, g_memcpy_func g_memcpy_l
, g_memset_func g_memset_l
, g_memmove_func g_memmove_l) {
#if USE_GLOBAL_FUNCTION_POINTERS

  /* If use global functions is turned on then the
  application must set the global functions before
  it does anything else or vpx_mem will have
  unpredictable results. */
  if (!g_func) {
    g_func = (struct GLOBAL_FUNC_POINTERS *)
             g_malloc_l(sizeof(struct GLOBAL_FUNC_POINTERS));

    if (!g_func) {
      return -1;
    }
  }

#if CONFIG_MEM_TRACKER
  {
    int rv = 0;
    rv = vpx_memory_tracker_set_functions(g_malloc_l
, g_calloc_l
, g_realloc_l
, g_free_l
, g_memcpy_l
, g_memset_l
, g_memmove_l);

    if (rv < 0) {
      return rv;
    }
  }
#endif

  g_func->g_malloc  = g_malloc_l;
  g_func->g_calloc  = g_calloc_l;
  g_func->g_realloc = g_realloc_l;
  g_func->g_free    = g_free_l;
  g_func->g_memcpy  = g_memcpy_l;
  g_func->g_memset  = g_memset_l;
  g_func->g_memmove = g_memmove_l;

  return 0;
#else
  (void)g_malloc_l;
  (void)g_calloc_l;
  (void)g_realloc_l;
  (void)g_free_l;
  (void)g_memcpy_l;
  (void)g_memset_l;
  (void)g_memmove_l;
  return -1;
#endif
}

int vpx_mem_unset_functions() {
#if USE_GLOBAL_FUNCTION_POINTERS

  if (g_func) {
    g_free_func temp_free = g_func->g_free;
    temp_free(g_func);
    g_func = NULL;
  }

#endif
  return 0;
}