/**
* Memory pool state (see @mp_push(), ...).
- ***/
+ * You should use this one as an opaque handle only, the insides are internal.
+ **/
struct mempool_state {
uns free[2];
void *last[2];
struct mempool_state *next;
};
-struct mempool { /** Memory pool. **/
+/**
+ * Memory pool.
+ * You should use this one as an opaque handle only, the insides are internal.
+ **/
+struct mempool {
struct mempool_state state;
void *unused, *last_big;
uns chunk_size, threshold, idx;
void mp_delete(struct mempool *pool);
/**
- * Free all data on a memory pool, but leaves it working.
+ * Frees all data on a memory pool, but leaves it working.
* It can keep some of the chunks allocated to serve
* further allocation requests. Leaves the @pool alive,
* even if it was created with @mp_new().
**/
void mp_restore(struct mempool *pool, struct mempool_state *state);
+/**
+ * Inlined version of @mp_restore().
+ **/
+static inline void mp_restore_fast(struct mempool *pool, struct mempool_state *state)
+{
+ if (pool->state.last[0] != state->last[0] || pool->state.last[1] != state->last[1])
+ mp_restore(pool, state);
+ else
+ {
+ pool->state = *state;
+ pool->last_big = &pool->last_big;
+ }
+}
+
/**
* Restore the state saved by the last call to @mp_push().
* @mp_pop() and @mp_push() works as a stack so you can push more states safely.
/**
* Concatenates two strings and stores result on @mp.
*/
-static inline char LIKE_MALLOC *mp_strcat(struct mempool *mp, const char *x, const char *y)
+static inline char *LIKE_MALLOC mp_strcat(struct mempool *mp, const char *x, const char *y)
{
return mp_multicat(mp, x, y, NULL);
}
/***
* [[format]]
- * Formated output
+ * Formatted output
* ---------------
***/
**/
char *mp_vprintf(struct mempool *mp, const char *fmt, va_list args) LIKE_MALLOC;
/**
- * Like @mp_printf(), but it appends the data at the end of memory
- * block pointed to by @ptr. The block is @mp_open()ed, so you have to
+ * Like @mp_printf(), but it appends the data at the end of string
+ * pointed to by @ptr. The string is @mp_open()ed, so you have to
* provide something that can be.
*
- * Returns pointer to the beginning of the block (the pointer may have
+ * Returns pointer to the beginning of the string (the pointer may have
* changed due to reallocation).
**/
char *mp_printf_append(struct mempool *mp, char *ptr, const char *fmt, ...) FORMAT_CHECK(printf,3,4);