forked from ebroder/python-zephyr
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
73 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#include "Python.h" | ||
|
||
#include <stdlib.h> | ||
|
||
#include "pool.h" | ||
|
||
void object_pool_init(struct object_pool *pool) { | ||
pool->objects = NULL; | ||
pool->count = pool->alloc = 0; | ||
} | ||
|
||
void object_pool_append(struct object_pool *pool, PyObject *obj) { | ||
if (pool->count == pool->alloc) { | ||
size_t new_alloc = pool->alloc ? 2 * pool->alloc : 8; | ||
pool->objects = realloc(pool->objects, new_alloc * sizeof(*pool->objects)); | ||
pool->alloc = new_alloc; | ||
} | ||
pool->objects[pool->count++] = obj; | ||
Py_INCREF(obj); | ||
} | ||
|
||
void object_pool_free(struct object_pool *pool) { | ||
int i; | ||
for (i = 0; i < pool->count; i++) { | ||
Py_DECREF(pool->objects[i]); | ||
} | ||
free(pool->objects); | ||
object_pool_init(pool); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
typedef struct object_pool { | ||
void **objects; | ||
size_t count; | ||
size_t alloc; | ||
} object_pool; | ||
|
||
void object_pool_init(struct object_pool *pool); | ||
void object_pool_append(struct object_pool *pool, PyObject *obj); | ||
void object_pool_free(struct object_pool *pool); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters