]> mj.ucw.cz Git - libucw.git/blobdiff - ucw/mempool.h
Mapping of whole files: Converted to size_t.
[libucw.git] / ucw / mempool.h
index a781503b9cc9fce0c3fb9f2a8f9d4ee57fc92fc3..8bf9bf9153d7152fe44dc08ecfb703b0ca762c95 100644 (file)
 #define _UCW_POOLS_H
 
 #include <ucw/alloc.h>
 #define _UCW_POOLS_H
 
 #include <ucw/alloc.h>
+#include <string.h>
 
 #ifdef CONFIG_UCW_CLEAN_ABI
 #define mp_alloc ucw_mp_alloc
 #define mp_alloc_internal ucw_mp_alloc_internal
 #define mp_alloc_noalign ucw_mp_alloc_noalign
 #define mp_alloc_zero ucw_mp_alloc_zero
 
 #ifdef CONFIG_UCW_CLEAN_ABI
 #define mp_alloc ucw_mp_alloc
 #define mp_alloc_internal ucw_mp_alloc_internal
 #define mp_alloc_noalign ucw_mp_alloc_noalign
 #define mp_alloc_zero ucw_mp_alloc_zero
+#define mp_append_printf ucw_mp_append_printf
+#define mp_append_vprintf ucw_mp_append_vprintf
 #define mp_delete ucw_mp_delete
 #define mp_flush ucw_mp_flush
 #define mp_grow_internal ucw_mp_grow_internal
 #define mp_delete ucw_mp_delete
 #define mp_flush ucw_mp_flush
 #define mp_grow_internal ucw_mp_grow_internal
@@ -28,7 +31,6 @@
 #define mp_open ucw_mp_open
 #define mp_pop ucw_mp_pop
 #define mp_printf ucw_mp_printf
 #define mp_open ucw_mp_open
 #define mp_pop ucw_mp_pop
 #define mp_printf ucw_mp_printf
-#define mp_printf_append ucw_mp_printf_append
 #define mp_push ucw_mp_push
 #define mp_realloc ucw_mp_realloc
 #define mp_realloc_zero ucw_mp_realloc_zero
 #define mp_push ucw_mp_push
 #define mp_realloc ucw_mp_realloc
 #define mp_realloc_zero ucw_mp_realloc_zero
@@ -43,7 +45,6 @@
 #define mp_strjoin ucw_mp_strjoin
 #define mp_total_size ucw_mp_total_size
 #define mp_vprintf ucw_mp_vprintf
 #define mp_strjoin ucw_mp_strjoin
 #define mp_total_size ucw_mp_total_size
 #define mp_vprintf ucw_mp_vprintf
-#define mp_vprintf_append ucw_mp_vprintf_append
 #endif
 
 /***
 #endif
 
 /***
@@ -57,7 +58,7 @@
  * You should use this one as an opaque handle only, the insides are internal.
  **/
 struct mempool_state {
  * You should use this one as an opaque handle only, the insides are internal.
  **/
 struct mempool_state {
-  uns free[2];
+  size_t free[2];
   void *last[2];
   struct mempool_state *next;
 };
   void *last[2];
   struct mempool_state *next;
 };
@@ -70,13 +71,16 @@ struct mempool {
   struct ucw_allocator allocator;
   struct mempool_state state;
   void *unused, *last_big;
   struct ucw_allocator allocator;
   struct mempool_state state;
   void *unused, *last_big;
-  uns chunk_size, threshold, idx;
+  size_t chunk_size, threshold;
+  uns idx;
+  u64 total_size;
 };
 
 struct mempool_stats {                 /** Mempool statistics. See @mp_stats(). **/
   u64 total_size;                      /* Real allocated size in bytes */
 };
 
 struct mempool_stats {                 /** Mempool statistics. See @mp_stats(). **/
   u64 total_size;                      /* Real allocated size in bytes */
+  u64 used_size;                       /* Estimated size allocated from mempool to application */
   uns chain_count[3];                  /* Number of allocated chunks in small/big/unused chains */
   uns chain_count[3];                  /* Number of allocated chunks in small/big/unused chains */
-  uns chain_size[3];                   /* Size of allocated chunks in small/big/unused chains */
+  u64 chain_size[3];                   /* Size of allocated chunks in small/big/unused chains */
 };
 
 /***
 };
 
 /***
@@ -87,13 +91,13 @@ struct mempool_stats {                      /** Mempool statistics. See @mp_stats(). **/
 
 /**
  * Initialize a given mempool structure.
 
 /**
  * Initialize a given mempool structure.
- * @chunk_size must be in the interval `[1, UINT_MAX / 2]`.
+ * @chunk_size must be in the interval `[1, SIZE_MAX / 2]`.
  * It will allocate memory by this large chunks and take
  * memory to satisfy requests from them.
  *
  * Memory pools can be treated as <<trans:respools,resources>>, see <<trans:res_mempool()>>.
  **/
  * It will allocate memory by this large chunks and take
  * memory to satisfy requests from them.
  *
  * Memory pools can be treated as <<trans:respools,resources>>, see <<trans:res_mempool()>>.
  **/
-void mp_init(struct mempool *pool, uns chunk_size);
+void mp_init(struct mempool *pool, size_t chunk_size);
 
 /**
  * Allocate and initialize a new memory pool.
 
 /**
  * Allocate and initialize a new memory pool.
@@ -103,7 +107,7 @@ void mp_init(struct mempool *pool, uns chunk_size);
  *
  * Memory pools can be treated as <<trans:respools,resources>>, see <<trans:res_mempool()>>.
  **/
  *
  * Memory pools can be treated as <<trans:respools,resources>>, see <<trans:res_mempool()>>.
  **/
-struct mempool *mp_new(uns chunk_size);
+struct mempool *mp_new(size_t chunk_size);
 
 /**
  * Cleanup mempool initialized by mp_init or mp_new.
 
 /**
  * Cleanup mempool initialized by mp_init or mp_new.
@@ -123,10 +127,22 @@ void mp_flush(struct mempool *pool);
 /**
  * Compute some statistics for debug purposes.
  * See the definition of the <<struct_mempool_stats,mempool_stats structure>>.
 /**
  * Compute some statistics for debug purposes.
  * See the definition of the <<struct_mempool_stats,mempool_stats structure>>.
+ * This function scans the chunk list, so it can be slow. If you are interested
+ * in total memory consumption only, mp_total_size() is faster.
  **/
 void mp_stats(struct mempool *pool, struct mempool_stats *stats);
  **/
 void mp_stats(struct mempool *pool, struct mempool_stats *stats);
-u64 mp_total_size(struct mempool *pool);       /** How many bytes were allocated by the pool. **/
 
 
+/**
+ * Return how many bytes were allocated by the pool, including unused parts
+ * of chunks. This function runs in constant time.
+ **/
+u64 mp_total_size(struct mempool *pool);
+
+/**
+ * Release unused chunks of memory reserved for further allocation
+ * requests, but stop if mp_total_size() would drop below @min_total_size.
+ **/
+void mp_shrink(struct mempool *pool, u64 min_total_size);
 
 /***
  * [[alloc]]
 
 /***
  * [[alloc]]
@@ -135,7 +151,7 @@ u64 mp_total_size(struct mempool *pool);    /** How many bytes were allocated by th
  ***/
 
 /* For internal use only, do not call directly */
  ***/
 
 /* For internal use only, do not call directly */
-void *mp_alloc_internal(struct mempool *pool, uns size) LIKE_MALLOC;
+void *mp_alloc_internal(struct mempool *pool, size_t size) LIKE_MALLOC;
 
 /**
  * The function allocates new @size bytes on a given memory pool.
 
 /**
  * The function allocates new @size bytes on a given memory pool.
@@ -147,24 +163,24 @@ void *mp_alloc_internal(struct mempool *pool, uns size) LIKE_MALLOC;
  * `CPU_STRUCT_ALIGN` bytes and this condition remains true also
  * after future reallocations.
  **/
  * `CPU_STRUCT_ALIGN` bytes and this condition remains true also
  * after future reallocations.
  **/
-void *mp_alloc(struct mempool *pool, uns size);
+void *mp_alloc(struct mempool *pool, size_t size);
 
 /**
  * The same as @mp_alloc(), but the result may be unaligned.
  **/
 
 /**
  * The same as @mp_alloc(), but the result may be unaligned.
  **/
-void *mp_alloc_noalign(struct mempool *pool, uns size);
+void *mp_alloc_noalign(struct mempool *pool, size_t size);
 
 /**
  * The same as @mp_alloc(), but fills the newly allocated memory with zeroes.
  **/
 
 /**
  * The same as @mp_alloc(), but fills the newly allocated memory with zeroes.
  **/
-void *mp_alloc_zero(struct mempool *pool, uns size);
+void *mp_alloc_zero(struct mempool *pool, size_t size);
 
 /**
  * Inlined version of @mp_alloc().
  **/
 
 /**
  * Inlined version of @mp_alloc().
  **/
-static inline void *mp_alloc_fast(struct mempool *pool, uns size)
+static inline void *mp_alloc_fast(struct mempool *pool, size_t size)
 {
 {
-  uns avail = pool->state.free[0] & ~(CPU_STRUCT_ALIGN - 1);
+  size_t avail = pool->state.free[0] & ~(size_t)(CPU_STRUCT_ALIGN - 1);
   if (size <= avail)
     {
       pool->state.free[0] = avail - size;
   if (size <= avail)
     {
       pool->state.free[0] = avail - size;
@@ -177,7 +193,7 @@ static inline void *mp_alloc_fast(struct mempool *pool, uns size)
 /**
  * Inlined version of @mp_alloc_noalign().
  **/
 /**
  * Inlined version of @mp_alloc_noalign().
  **/
-static inline void *mp_alloc_fast_noalign(struct mempool *pool, uns size)
+static inline void *mp_alloc_fast_noalign(struct mempool *pool, size_t size)
 {
   if (size <= pool->state.free[0])
     {
 {
   if (size <= pool->state.free[0])
     {
@@ -210,9 +226,9 @@ static inline struct ucw_allocator *mp_get_allocator(struct mempool *mp)
  ***/
 
 /* For internal use only, do not call directly */
  ***/
 
 /* For internal use only, do not call directly */
-void *mp_start_internal(struct mempool *pool, uns size) LIKE_MALLOC;
-void *mp_grow_internal(struct mempool *pool, uns size);
-void *mp_spread_internal(struct mempool *pool, void *p, uns size);
+void *mp_start_internal(struct mempool *pool, size_t size) LIKE_MALLOC;
+void *mp_grow_internal(struct mempool *pool, size_t size);
+void *mp_spread_internal(struct mempool *pool, void *p, size_t size);
 
 static inline uns mp_idx(struct mempool *pool, void *ptr)
 {
 
 static inline uns mp_idx(struct mempool *pool, void *ptr)
 {
@@ -232,15 +248,15 @@ static inline uns mp_idx(struct mempool *pool, void *ptr)
  * Keep in mind that you can't make any other pool allocations
  * before you "close" the growing buffer with @mp_end().
  */
  * Keep in mind that you can't make any other pool allocations
  * before you "close" the growing buffer with @mp_end().
  */
-void *mp_start(struct mempool *pool, uns size);
-void *mp_start_noalign(struct mempool *pool, uns size);
+void *mp_start(struct mempool *pool, size_t size);
+void *mp_start_noalign(struct mempool *pool, size_t size);
 
 /**
  * Inlined version of @mp_start().
  **/
 
 /**
  * Inlined version of @mp_start().
  **/
-static inline void *mp_start_fast(struct mempool *pool, uns size)
+static inline void *mp_start_fast(struct mempool *pool, size_t size)
 {
 {
-  uns avail = pool->state.free[0] & ~(CPU_STRUCT_ALIGN - 1);
+  size_t avail = pool->state.free[0] & ~(size_t)(CPU_STRUCT_ALIGN - 1);
   if (size <= avail)
     {
       pool->idx = 0;
   if (size <= avail)
     {
       pool->idx = 0;
@@ -254,7 +270,7 @@ static inline void *mp_start_fast(struct mempool *pool, uns size)
 /**
  * Inlined version of @mp_start_noalign().
  **/
 /**
  * Inlined version of @mp_start_noalign().
  **/
-static inline void *mp_start_fast_noalign(struct mempool *pool, uns size)
+static inline void *mp_start_fast_noalign(struct mempool *pool, size_t size)
 {
   if (size <= pool->state.free[0])
     {
 {
   if (size <= pool->state.free[0])
     {
@@ -277,7 +293,7 @@ static inline void *mp_ptr(struct mempool *pool)
  * Return the number of bytes available for extending the growing buffer.
  * (Before a reallocation will be needed).
  **/
  * Return the number of bytes available for extending the growing buffer.
  * (Before a reallocation will be needed).
  **/
-static inline uns mp_avail(struct mempool *pool)
+static inline size_t mp_avail(struct mempool *pool)
 {
   return pool->state.free[pool->idx];
 }
 {
   return pool->state.free[pool->idx];
 }
@@ -288,7 +304,7 @@ static inline uns mp_avail(struct mempool *pool)
  * change its starting position. The content will be unchanged to the minimum
  * of the old and new sizes; newly allocated memory will be uninitialized.
  * Multiple calls to mp_grow() have amortized linear cost wrt. the maximum value of @size. */
  * change its starting position. The content will be unchanged to the minimum
  * of the old and new sizes; newly allocated memory will be uninitialized.
  * Multiple calls to mp_grow() have amortized linear cost wrt. the maximum value of @size. */
-static inline void *mp_grow(struct mempool *pool, uns size)
+static inline void *mp_grow(struct mempool *pool, size_t size)
 {
   return (size <= mp_avail(pool)) ? mp_ptr(pool) : mp_grow_internal(pool, size);
 }
 {
   return (size <= mp_avail(pool)) ? mp_ptr(pool) : mp_grow_internal(pool, size);
 }
@@ -305,9 +321,43 @@ static inline void *mp_expand(struct mempool *pool)
  * Ensure that there is at least @size bytes free after @p,
  * if not, reallocate and adjust @p.
  **/
  * Ensure that there is at least @size bytes free after @p,
  * if not, reallocate and adjust @p.
  **/
-static inline void *mp_spread(struct mempool *pool, void *p, uns size)
+static inline void *mp_spread(struct mempool *pool, void *p, size_t size)
+{
+  return (((size_t)((byte *)pool->state.last[pool->idx] - (byte *)p) >= size) ? p : mp_spread_internal(pool, p, size));
+}
+
+/**
+ * Append a character to the growing buffer. Called with @p pointing after
+ * the last byte in the buffer, returns a pointer after the last byte
+ * of the new (possibly reallocated) buffer.
+ **/
+static inline char *mp_append_char(struct mempool *pool, char *p, uns c)
+{
+  p = mp_spread(pool, p, 1);
+  *p++ = c;
+  return p;
+}
+
+/**
+ * Append a memory block to the growing buffer. Called with @p pointing after
+ * the last byte in the buffer, returns a pointer after the last byte
+ * of the new (possibly reallocated) buffer.
+ **/
+static inline void *mp_append_block(struct mempool *pool, void *p, const void *block, size_t size)
+{
+  char *q = mp_spread(pool, p, size);
+  memcpy(q, block, size);
+  return q + size;
+}
+
+/**
+ * Append a string to the growing buffer. Called with @p pointing after
+ * the last byte in the buffer, returns a pointer after the last byte
+ * of the new (possibly reallocated) buffer.
+ **/
+static inline void *mp_append_string(struct mempool *pool, void *p, const char *str)
 {
 {
-  return (((uns)((byte *)pool->state.last[pool->idx] - (byte *)p) >= size) ? p : mp_spread_internal(pool, p, size));
+  return mp_append_block(pool, p, str, strlen(str));
 }
 
 /**
 }
 
 /**
@@ -322,10 +372,19 @@ static inline void *mp_end(struct mempool *pool, void *end)
   return p;
 }
 
   return p;
 }
 
+/**
+ * Close the growing buffer as a string. That is, append a zero byte and call mp_end().
+ **/
+static inline char *mp_end_string(struct mempool *pool, void *end)
+{
+  end = mp_append_char(pool, end, 0);
+  return mp_end(pool, end);
+}
+
 /**
  * Return size in bytes of the last allocated memory block (with @mp_alloc() or @mp_end()).
  **/
 /**
  * Return size in bytes of the last allocated memory block (with @mp_alloc() or @mp_end()).
  **/
-static inline uns mp_size(struct mempool *pool, void *ptr)
+static inline size_t mp_size(struct mempool *pool, void *ptr)
 {
   uns idx = mp_idx(pool, ptr);
   return ((byte *)pool->state.last[idx] - (byte *)ptr) - pool->state.free[idx];
 {
   uns idx = mp_idx(pool, ptr);
   return ((byte *)pool->state.last[idx] - (byte *)ptr) - pool->state.free[idx];
@@ -336,15 +395,15 @@ static inline uns mp_size(struct mempool *pool, void *ptr)
  * for growing and return its size in bytes. The contents and the start pointer
  * remain unchanged. Do not forget to call @mp_end() to close it.
  **/
  * for growing and return its size in bytes. The contents and the start pointer
  * remain unchanged. Do not forget to call @mp_end() to close it.
  **/
-uns mp_open(struct mempool *pool, void *ptr);
+size_t mp_open(struct mempool *pool, void *ptr);
 
 /**
  * Inlined version of @mp_open().
  **/
 
 /**
  * Inlined version of @mp_open().
  **/
-static inline uns mp_open_fast(struct mempool *pool, void *ptr)
+static inline size_t mp_open_fast(struct mempool *pool, void *ptr)
 {
   pool->idx = mp_idx(pool, ptr);
 {
   pool->idx = mp_idx(pool, ptr);
-  uns size = ((byte *)pool->state.last[pool->idx] - (byte *)ptr) - pool->state.free[pool->idx];
+  size_t size = ((byte *)pool->state.last[pool->idx] - (byte *)ptr) - pool->state.free[pool->idx];
   pool->state.free[pool->idx] += size;
   return size;
 }
   pool->state.free[pool->idx] += size;
   return size;
 }
@@ -354,17 +413,17 @@ static inline uns mp_open_fast(struct mempool *pool, void *ptr)
  * to the new @size. Behavior is similar to @mp_grow(), but the resulting
  * block is closed.
  **/
  * to the new @size. Behavior is similar to @mp_grow(), but the resulting
  * block is closed.
  **/
-void *mp_realloc(struct mempool *pool, void *ptr, uns size);
+void *mp_realloc(struct mempool *pool, void *ptr, size_t size);
 
 /**
  * The same as @mp_realloc(), but fills the additional bytes (if any) with zeroes.
  **/
 
 /**
  * The same as @mp_realloc(), but fills the additional bytes (if any) with zeroes.
  **/
-void *mp_realloc_zero(struct mempool *pool, void *ptr, uns size);
+void *mp_realloc_zero(struct mempool *pool, void *ptr, size_t size);
 
 /**
  * Inlined version of @mp_realloc().
  **/
 
 /**
  * Inlined version of @mp_realloc().
  **/
-static inline void *mp_realloc_fast(struct mempool *pool, void *ptr, uns size)
+static inline void *mp_realloc_fast(struct mempool *pool, void *ptr, size_t size)
 {
   mp_open_fast(pool, ptr);
   ptr = mp_grow(pool, size);
 {
   mp_open_fast(pool, ptr);
   ptr = mp_grow(pool, size);
@@ -432,7 +491,7 @@ void mp_pop(struct mempool *pool);
  ***/
 
 char *mp_strdup(struct mempool *, const char *) LIKE_MALLOC;           /** Makes a copy of a string on a mempool. Returns NULL for NULL string. **/
  ***/
 
 char *mp_strdup(struct mempool *, const char *) LIKE_MALLOC;           /** Makes a copy of a string on a mempool. Returns NULL for NULL string. **/
-void *mp_memdup(struct mempool *, const void *, uns) LIKE_MALLOC;      /** Makes a copy of a memory block on a mempool. **/
+void *mp_memdup(struct mempool *, const void *, size_t) LIKE_MALLOC;   /** Makes a copy of a memory block on a mempool. **/
 /**
  * Concatenates all passed strings. The last parameter must be NULL.
  * This will concatenate two strings:
 /**
  * Concatenates all passed strings. The last parameter must be NULL.
  * This will concatenate two strings:
@@ -457,7 +516,7 @@ char *mp_strjoin(struct mempool *p, char **a, uns n, uns sep) LIKE_MALLOC;
  * Convert memory block to a string. Makes a copy of the given memory block
  * in the mempool @p, adding an extra terminating zero byte at the end.
  **/
  * Convert memory block to a string. Makes a copy of the given memory block
  * in the mempool @p, adding an extra terminating zero byte at the end.
  **/
-char *mp_str_from_mem(struct mempool *p, const void *mem, uns len) LIKE_MALLOC;
+char *mp_str_from_mem(struct mempool *p, const void *mem, size_t len) LIKE_MALLOC;
 
 
 /***
 
 
 /***
@@ -481,11 +540,19 @@ char *mp_vprintf(struct mempool *mp, const char *fmt, va_list args) LIKE_MALLOC;
  *
  * Returns pointer to the beginning of the string (the pointer may have
  * changed due to reallocation).
  *
  * Returns pointer to the beginning of the string (the pointer may have
  * changed due to reallocation).
+ *
+ * Alternatively, this function may be called mp_printf_append() for compatibility with
+ * previous releases of LibUCW.
  **/
  **/
-char *mp_printf_append(struct mempool *mp, char *ptr, const char *fmt, ...) FORMAT_CHECK(printf,3,4);
+char *mp_append_printf(struct mempool *mp, char *ptr, const char *fmt, ...) FORMAT_CHECK(printf,3,4);
+#define mp_printf_append mp_append_printf
 /**
 /**
- * Like @mp_printf_append(), but uses `va_list` for parameters.
+ * Like @mp_append_printf(), but uses `va_list` for parameters.
+ *
+ * Alternatively, this function may be called mp_vprintf_append() for compatibility with
+ * previous releases of LibUCW.
  **/
  **/
-char *mp_vprintf_append(struct mempool *mp, char *ptr, const char *fmt, va_list args);
+char *mp_append_vprintf(struct mempool *mp, char *ptr, const char *fmt, va_list args);
+#define mp_vprintf_append mp_append_vprintf
 
 #endif
 
 #endif