]> mj.ucw.cz Git - libucw.git/blobdiff - ucw/mempool.h
option parser: Empty stub of the documentation
[libucw.git] / ucw / mempool.h
index 63b4793791e1a2ae67e0e5da344676ffffcffe79..350054e3fc481d0eb8a8879daabb1c1d021f7178 100644 (file)
 
 /**
  * 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;
@@ -49,6 +54,8 @@ struct mempool_stats {                        /** Mempool statistics. See @mp_stats(). **/
  * @chunk_size must be in the interval `[1, UINT_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()>>.
  **/
 void mp_init(struct mempool *pool, uns chunk_size);
 
@@ -57,6 +64,8 @@ void mp_init(struct mempool *pool, uns chunk_size);
  * See @mp_init() for @chunk_size limitations.
  *
  * The new mempool structure is allocated on the new mempool.
+ *
+ * Memory pools can be treated as <<trans:respools,resources>>, see <<trans:res_mempool()>>.
  **/
 struct mempool *mp_new(uns chunk_size);
 
@@ -68,7 +77,7 @@ struct mempool *mp_new(uns chunk_size);
 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().
@@ -123,7 +132,7 @@ static inline void *mp_alloc_fast(struct mempool *pool, uns size)
   if (size <= avail)
     {
       pool->state.free[0] = avail - size;
-      return pool->state.last[0] - avail;
+      return (byte *)pool->state.last[0] - avail;
     }
   else
     return mp_alloc_internal(pool, size);
@@ -136,7 +145,7 @@ static inline void *mp_alloc_fast_noalign(struct mempool *pool, uns size)
 {
   if (size <= pool->state.free[0])
     {
-      void *ptr = pool->state.last[0] - pool->state.free[0];
+      void *ptr = (byte *)pool->state.last[0] - pool->state.free[0];
       pool->state.free[0] -= size;
       return ptr;
     }
@@ -152,6 +161,8 @@ static inline void *mp_alloc_fast_noalign(struct mempool *pool, uns size)
  * You do not need to know, how a buffer will need to be large,
  * you can grow it incrementally to needed size. You can grow only
  * one buffer at a time on a given mempool.
+ *
+ * Similar functionality is provided by <<growbuf:,growing buffes>> module.
  ***/
 
 /* For internal use only, do not call directly */
@@ -191,7 +202,7 @@ static inline void *mp_start_fast(struct mempool *pool, uns size)
     {
       pool->idx = 0;
       pool->state.free[0] = avail;
-      return pool->state.last[0] - avail;
+      return (byte *)pool->state.last[0] - avail;
     }
   else
     return mp_start_internal(pool, size);
@@ -205,7 +216,7 @@ static inline void *mp_start_fast_noalign(struct mempool *pool, uns size)
   if (size <= pool->state.free[0])
     {
       pool->idx = 0;
-      return pool->state.last[0] - pool->state.free[0];
+      return (byte *)pool->state.last[0] - pool->state.free[0];
     }
   else
     return mp_start_internal(pool, size);
@@ -216,7 +227,7 @@ static inline void *mp_start_fast_noalign(struct mempool *pool, uns size)
  **/
 static inline void *mp_ptr(struct mempool *pool)
 {
-  return pool->state.last[pool->idx] - pool->state.free[pool->idx];
+  return (byte *)pool->state.last[pool->idx] - pool->state.free[pool->idx];
 }
 
 /**
@@ -253,7 +264,7 @@ static inline void *mp_expand(struct mempool *pool)
  **/
 static inline void *mp_spread(struct mempool *pool, void *p, uns size)
 {
-  return (((uns)(pool->state.last[pool->idx] - p) >= size) ? p : mp_spread_internal(pool, p, size));
+  return (((uns)((byte *)pool->state.last[pool->idx] - (byte *)p) >= size) ? p : mp_spread_internal(pool, p, size));
 }
 
 /**
@@ -264,7 +275,7 @@ static inline void *mp_spread(struct mempool *pool, void *p, uns size)
 static inline void *mp_end(struct mempool *pool, void *end)
 {
   void *p = mp_ptr(pool);
-  pool->state.free[pool->idx] = pool->state.last[pool->idx] - end;
+  pool->state.free[pool->idx] = (byte *)pool->state.last[pool->idx] - (byte *)end;
   return p;
 }
 
@@ -274,7 +285,7 @@ static inline void *mp_end(struct mempool *pool, void *end)
 static inline uns mp_size(struct mempool *pool, void *ptr)
 {
   uns idx = mp_idx(pool, ptr);
-  return pool->state.last[idx] - ptr - pool->state.free[idx];
+  return ((byte *)pool->state.last[idx] - (byte *)ptr) - pool->state.free[idx];
 }
 
 /**
@@ -290,7 +301,7 @@ uns mp_open(struct mempool *pool, void *ptr);
 static inline uns mp_open_fast(struct mempool *pool, void *ptr)
 {
   pool->idx = mp_idx(pool, ptr);
-  uns size = pool->state.last[pool->idx] - ptr - pool->state.free[pool->idx];
+  uns size = ((byte *)pool->state.last[pool->idx] - (byte *)ptr) - pool->state.free[pool->idx];
   pool->state.free[pool->idx] += size;
   return size;
 }
@@ -314,7 +325,7 @@ static inline void *mp_realloc_fast(struct mempool *pool, void *ptr, uns size)
 {
   mp_open_fast(pool, ptr);
   ptr = mp_grow(pool, size);
-  mp_end(pool, ptr + size);
+  mp_end(pool, (byte *)ptr + size);
   return ptr;
 }
 
@@ -329,7 +340,7 @@ static inline void *mp_realloc_fast(struct mempool *pool, void *ptr, uns size)
 
 /**
  * Save the current state of a memory pool.
- * Do not call this function with an opened growing buffer. 
+ * Do not call this function with an opened growing buffer.
  **/
 static inline void mp_save(struct mempool *pool, struct mempool_state *state)
 {
@@ -350,6 +361,20 @@ struct mempool_state *mp_push(struct mempool *pool);
  **/
 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.
@@ -363,7 +388,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. **/
+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. **/
 /**
  * Concatenates all passed strings. The last parameter must be NULL.
@@ -385,11 +410,16 @@ static inline char *LIKE_MALLOC mp_strcat(struct mempool *mp, const char *x, con
  * tells how many there is of them.
  **/
 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.
+ **/
+char *mp_str_from_mem(struct mempool *p, const void *mem, uns len) LIKE_MALLOC;
 
 
 /***
  * [[format]]
- * Formated output
+ * Formatted output
  * ---------------
  ***/
 
@@ -402,11 +432,11 @@ char *mp_printf(struct mempool *mp, const char *fmt, ...) FORMAT_CHECK(printf,2,
  **/
 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);