Mercurial > libavcodec.hg
comparison utils.c @ 1900:5cde80c5d929 libavcodec
static allocation rewrite (old code was plain a broken mess)
doesnt call realloc every time
doesnt randomly overwrite memory after after 8-16 calls
doesnt use ugly macro wraper
fewer lines of code
| author | michael |
|---|---|
| date | Sun, 21 Mar 2004 21:58:14 +0000 |
| parents | ea2a4058441c |
| children | bc5039adb9af |
comparison
equal
deleted
inserted
replaced
| 1899:379a18a7131f | 1900:5cde80c5d929 |
|---|---|
| 64 | 64 |
| 65 return av_realloc(ptr, *size); | 65 return av_realloc(ptr, *size); |
| 66 } | 66 } |
| 67 | 67 |
| 68 | 68 |
| 69 /* allocation of static arrays - do not use for normal allocation */ | |
| 70 static unsigned int last_static = 0; | 69 static unsigned int last_static = 0; |
| 71 static char*** array_static = NULL; | 70 static unsigned int allocated_static = 0; |
| 71 static void** array_static = NULL; | |
| 72 static const unsigned int grow_static = 64; // ^2 | 72 static const unsigned int grow_static = 64; // ^2 |
| 73 void *__av_mallocz_static(void** location, unsigned int size) | 73 |
| 74 { | 74 /** |
| 75 unsigned int l = (last_static + grow_static) & ~(grow_static - 1); | 75 * allocation of static arrays - do not use for normal allocation. |
| 76 */ | |
| 77 void *av_mallocz_static(unsigned int size) | |
| 78 { | |
| 76 void *ptr = av_mallocz(size); | 79 void *ptr = av_mallocz(size); |
| 77 if (!ptr) | 80 |
| 78 return NULL; | 81 if(ptr){ |
| 79 | 82 array_static =av_fast_realloc(array_static, &allocated_static, last_static+1); |
| 80 if (location) | 83 array_static[last_static++] = ptr; |
| 81 { | 84 } |
| 82 if (l > last_static) | 85 |
| 83 array_static = av_realloc(array_static, l); | |
| 84 array_static[last_static++] = (char**) location; | |
| 85 *location = ptr; | |
| 86 } | |
| 87 return ptr; | 86 return ptr; |
| 88 } | 87 } |
| 89 /* free all static arrays and reset pointers to 0 */ | 88 |
| 89 /** | |
| 90 * free all static arrays and reset pointers to 0. | |
| 91 */ | |
| 90 void av_free_static(void) | 92 void av_free_static(void) |
| 91 { | 93 { |
| 92 if (array_static) | 94 while(last_static){ |
| 93 { | 95 av_freep(&array_static[--last_static]); |
| 94 unsigned i; | 96 } |
| 95 for (i = 0; i < last_static; i++) | 97 av_freep(&array_static); |
| 96 { | |
| 97 av_free(*array_static[i]); | |
| 98 *array_static[i] = NULL; | |
| 99 } | |
| 100 av_free(array_static); | |
| 101 array_static = 0; | |
| 102 } | |
| 103 last_static = 0; | |
| 104 } | 98 } |
| 105 | 99 |
| 106 /** | 100 /** |
| 107 * Frees memory and sets the pointer to NULL. | 101 * Frees memory and sets the pointer to NULL. |
| 108 * @param arg pointer to the pointer which should be freed | 102 * @param arg pointer to the pointer which should be freed |
