mirror of
https://github.com/moparisthebest/pacman
synced 2024-12-22 07:48:50 -05:00
util: Add _alpm_realloc() and _alpm_greedy_grow()
These will be used in the following patches. Signed-off-by: Florian Pritz <bluewind@xinu.at>
This commit is contained in:
parent
f27fad9b89
commit
42f4a5081e
@ -1287,6 +1287,68 @@ int _alpm_fnmatch(const void *pattern, const void *string)
|
||||
return fnmatch(pattern, string, 0);
|
||||
}
|
||||
|
||||
/** Think of this as realloc with error handling. If realloc fails NULL will be
|
||||
* returned and data will not be changed.
|
||||
*
|
||||
* Newly created memory will be zeroed.
|
||||
*
|
||||
* @param data source memory space
|
||||
* @param current size of the space pointed to by data
|
||||
* @param required size you want
|
||||
* @return new memory; NULL on error
|
||||
*/
|
||||
void *_alpm_realloc(void **data, size_t *current, const size_t required)
|
||||
{
|
||||
char *newdata;
|
||||
|
||||
newdata = realloc(*data, required);
|
||||
if(!newdata) {
|
||||
_alpm_alloc_fail(required);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (*current < required) {
|
||||
/* ensure all new memory is zeroed out, in both the initial
|
||||
* allocation and later reallocs */
|
||||
memset(newdata + *current, 0, required - *current);
|
||||
}
|
||||
*current = required;
|
||||
*data = newdata;
|
||||
return newdata;
|
||||
}
|
||||
|
||||
/** This automatically grows data based on current/required.
|
||||
*
|
||||
* The memory space will be initialised to required bytes and doubled in size when required.
|
||||
*
|
||||
* Newly created memory will be zeroed.
|
||||
* @param data source memory space
|
||||
* @param current size of the space pointed to by data
|
||||
* @param required size you want
|
||||
* @return new memory if grown; old memory otherwise; NULL on error
|
||||
*/
|
||||
void *_alpm_greedy_grow(void **data, size_t *current, const size_t required)
|
||||
{
|
||||
size_t newsize = 0;
|
||||
|
||||
if(*current >= required) {
|
||||
return data;
|
||||
}
|
||||
|
||||
if(*current == 0) {
|
||||
newsize = required;
|
||||
} else {
|
||||
newsize = *current * 2;
|
||||
}
|
||||
|
||||
/* check for overflows */
|
||||
if (newsize < required) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return _alpm_realloc(data, current, required);
|
||||
}
|
||||
|
||||
void _alpm_alloc_fail(size_t size)
|
||||
{
|
||||
fprintf(stderr, "alloc failure: could not allocate %zd bytes\n", size);
|
||||
|
@ -141,6 +141,8 @@ int _alpm_raw_ncmp(const char *first, const char *second, size_t max);
|
||||
int _alpm_access(alpm_handle_t *handle, const char *dir, const char *file, int amode);
|
||||
int _alpm_fnmatch_patterns(alpm_list_t *patterns, const char *string);
|
||||
int _alpm_fnmatch(const void *pattern, const void *string);
|
||||
void *_alpm_realloc(void **data, size_t *current, const size_t required);
|
||||
void *_alpm_greedy_grow(void **data, size_t *current, const size_t required);
|
||||
|
||||
#ifndef HAVE_STRSEP
|
||||
char *strsep(char **, const char *);
|
||||
|
Loading…
Reference in New Issue
Block a user