"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "src/data-pool.c" between
libmaxminddb-1.5.0.tar.gz and libmaxminddb-1.5.2.tar.gz

About: libmaxminddb is a library that provides functions for working with MaxMind DB files (free GeoLite 2 or commercial GeoIP2).

data-pool.c  (libmaxminddb-1.5.0):data-pool.c  (libmaxminddb-1.5.2)
skipping to change at line 12 skipping to change at line 12
#include "maxminddb.h" #include "maxminddb.h"
#include <stdbool.h> #include <stdbool.h>
#include <stddef.h> #include <stddef.h>
#include <stdlib.h> #include <stdlib.h>
static bool can_multiply(size_t const, size_t const, size_t const); static bool can_multiply(size_t const, size_t const, size_t const);
// Allocate an MMDB_data_pool_s. It initially has space for size // Allocate an MMDB_data_pool_s. It initially has space for size
// MMDB_entry_data_list_s structs. // MMDB_entry_data_list_s structs.
MMDB_data_pool_s *data_pool_new(size_t const size) MMDB_data_pool_s *data_pool_new(size_t const size) {
{
MMDB_data_pool_s *const pool = calloc(1, sizeof(MMDB_data_pool_s)); MMDB_data_pool_s *const pool = calloc(1, sizeof(MMDB_data_pool_s));
if (!pool) { if (!pool) {
return NULL; return NULL;
} }
if (size == 0 || if (size == 0 ||
!can_multiply(SIZE_MAX, size, sizeof(MMDB_entry_data_list_s))) { !can_multiply(SIZE_MAX, size, sizeof(MMDB_entry_data_list_s))) {
data_pool_destroy(pool); data_pool_destroy(pool);
return NULL; return NULL;
} }
skipping to change at line 43 skipping to change at line 42
pool->block = pool->blocks[0]; pool->block = pool->blocks[0];
return pool; return pool;
} }
// Determine if we can multiply m*n. We can do this if the result will be below // Determine if we can multiply m*n. We can do this if the result will be below
// the given max. max will typically be SIZE_MAX. // the given max. max will typically be SIZE_MAX.
// //
// We want to know if we'll wrap around. // We want to know if we'll wrap around.
static bool can_multiply(size_t const max, size_t const m, size_t const n) static bool can_multiply(size_t const max, size_t const m, size_t const n) {
{
if (m == 0) { if (m == 0) {
return false; return false;
} }
return n <= max / m; return n <= max / m;
} }
// Clean up the data pool. // Clean up the data pool.
void data_pool_destroy(MMDB_data_pool_s *const pool) void data_pool_destroy(MMDB_data_pool_s *const pool) {
{
if (!pool) { if (!pool) {
return; return;
} }
for (size_t i = 0; i <= pool->index; i++) { for (size_t i = 0; i <= pool->index; i++) {
free(pool->blocks[i]); free(pool->blocks[i]);
} }
free(pool); free(pool);
} }
// Claim a new struct from the pool. Doing this may cause the pool's size to // Claim a new struct from the pool. Doing this may cause the pool's size to
// grow. // grow.
MMDB_entry_data_list_s *data_pool_alloc(MMDB_data_pool_s *const pool) MMDB_entry_data_list_s *data_pool_alloc(MMDB_data_pool_s *const pool) {
{
if (!pool) { if (!pool) {
return NULL; return NULL;
} }
if (pool->used < pool->size) { if (pool->used < pool->size) {
MMDB_entry_data_list_s *const element = pool->block + pool->used; MMDB_entry_data_list_s *const element = pool->block + pool->used;
pool->used++; pool->used++;
return element; return element;
} }
skipping to change at line 118 skipping to change at line 114
pool->sizes[pool->index] = pool->size; pool->sizes[pool->index] = pool->size;
MMDB_entry_data_list_s *const element = pool->block; MMDB_entry_data_list_s *const element = pool->block;
pool->used = 1; pool->used = 1;
return element; return element;
} }
// Turn the structs in the array-like pool into a linked list. // Turn the structs in the array-like pool into a linked list.
// //
// Before calling this function, the list isn't linked up. // Before calling this function, the list isn't linked up.
MMDB_entry_data_list_s *data_pool_to_list(MMDB_data_pool_s *const pool) MMDB_entry_data_list_s *data_pool_to_list(MMDB_data_pool_s *const pool) {
{
if (!pool) { if (!pool) {
return NULL; return NULL;
} }
if (pool->index == 0 && pool->used == 0) { if (pool->index == 0 && pool->used == 0) {
return NULL; return NULL;
} }
for (size_t i = 0; i <= pool->index; i++) { for (size_t i = 0; i <= pool->index; i++) {
MMDB_entry_data_list_s *const block = pool->blocks[i]; MMDB_entry_data_list_s *const block = pool->blocks[i];
skipping to change at line 157 skipping to change at line 152
return pool->blocks[0]; return pool->blocks[0];
} }
#ifdef TEST_DATA_POOL #ifdef TEST_DATA_POOL
#include <libtap/tap.h> #include <libtap/tap.h>
#include <maxminddb_test_helper.h> #include <maxminddb_test_helper.h>
static void test_can_multiply(void); static void test_can_multiply(void);
int main(void) int main(void) {
{
plan(NO_PLAN); plan(NO_PLAN);
test_can_multiply(); test_can_multiply();
done_testing(); done_testing();
} }
static void test_can_multiply(void) static void test_can_multiply(void) {
{ { ok(can_multiply(SIZE_MAX, 1, SIZE_MAX), "1*SIZE_MAX is ok"); }
{
ok(can_multiply(SIZE_MAX, 1, SIZE_MAX), "1*SIZE_MAX is ok");
}
{ { ok(!can_multiply(SIZE_MAX, 2, SIZE_MAX), "2*SIZE_MAX is not ok"); }
ok(!can_multiply(SIZE_MAX, 2, SIZE_MAX), "2*SIZE_MAX is not ok");
}
{ {
ok(can_multiply(SIZE_MAX, 10240, sizeof(MMDB_entry_data_list_s)), ok(can_multiply(SIZE_MAX, 10240, sizeof(MMDB_entry_data_list_s)),
"1024 entry_data_list_s's are okay"); "1024 entry_data_list_s's are okay");
} }
} }
#endif #endif
 End of changes. 8 change blocks. 
20 lines changed or deleted 9 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)