comparison utils.c @ 1031:19de1445beb2 libavcodec

use av_malloc() functions - added av_strdup and av_realloc()
author bellard
date Thu, 23 Jan 2003 23:03:09 +0000
parents 6129c88a6393
children 8f440ca8e0b0
comparison
equal deleted inserted replaced
1030:801f2739264a 1031:19de1445beb2
31 return NULL; 31 return NULL;
32 memset(ptr, 0, size); 32 memset(ptr, 0, size);
33 return ptr; 33 return ptr;
34 } 34 }
35 35
36 char *av_strdup(const char *s)
37 {
38 char *ptr;
39 int len;
40 len = strlen(s) + 1;
41 ptr = av_malloc(len);
42 if (!ptr)
43 return NULL;
44 memcpy(ptr, s, len);
45 return ptr;
46 }
47
48 /**
49 * realloc which does nothing if the block is large enough
50 */
51 void *av_fast_realloc(void *ptr, int *size, int min_size)
52 {
53 if(min_size < *size)
54 return ptr;
55
56 *size= min_size + 10*1024;
57
58 return av_realloc(ptr, *size);
59 }
60
61
36 /* allocation of static arrays - do not use for normal allocation */ 62 /* allocation of static arrays - do not use for normal allocation */
37 static unsigned int last_static = 0; 63 static unsigned int last_static = 0;
38 static char*** array_static = NULL; 64 static char*** array_static = NULL;
39 static const unsigned int grow_static = 64; // ^2 65 static const unsigned int grow_static = 64; // ^2
40 void *__av_mallocz_static(void** location, unsigned int size) 66 void *__av_mallocz_static(void** location, unsigned int size)
45 return NULL; 71 return NULL;
46 72
47 if (location) 73 if (location)
48 { 74 {
49 if (l > last_static) 75 if (l > last_static)
50 array_static = realloc(array_static, l); 76 array_static = av_realloc(array_static, l);
51 array_static[last_static++] = (char**) location; 77 array_static[last_static++] = (char**) location;
52 *location = ptr; 78 *location = ptr;
53 } 79 }
54 return ptr; 80 return ptr;
55 } 81 }
59 if (array_static) 85 if (array_static)
60 { 86 {
61 unsigned i; 87 unsigned i;
62 for (i = 0; i < last_static; i++) 88 for (i = 0; i < last_static; i++)
63 { 89 {
64 free(*array_static[i]); 90 av_free(*array_static[i]);
65 *array_static[i] = NULL; 91 *array_static[i] = NULL;
66 } 92 }
67 free(array_static); 93 av_free(array_static);
68 array_static = 0; 94 array_static = 0;
69 } 95 }
70 last_static = 0; 96 last_static = 0;
71 } 97 }
72 98