/[LeafOK_CVS]/lbbs/src/memory_pool.c
ViewVC logotype

Diff of /lbbs/src/memory_pool.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 1.2 by sysadm, Fri Jun 13 13:39:46 2025 UTC Revision 1.12 by sysadm, Tue Jan 6 13:59:21 2026 UTC
# Line 1  Line 1 
1  /***************************************************************************  /* SPDX-License-Identifier: GPL-3.0-or-later */
2                                                  memory_pool.c  -  description  /*
3                                                           -------------------   * memory_pool
4          copyright            : (C) 2004-2025 by Leaflet   *   - memory pool
5          email                : leaflet@leafok.com   *
6   ***************************************************************************/   * Copyright (C) 2004-2026  Leaflet <leaflet@leafok.com>
7     */
8  /***************************************************************************  
9   *                                                                         *  #ifdef HAVE_CONFIG_H
10   *   This program is free software; you can redistribute it and/or modify  *  #include "config.h"
11   *   it under the terms of the GNU General Public License as published by  *  #endif
  *   the Free Software Foundation; either version 3 of the License, or     *  
  *   (at your option) any later version.                                   *  
  *                                                                         *  
  ***************************************************************************/  
12    
 #include "memory_pool.h"  
13  #include "log.h"  #include "log.h"
14    #include "memory_pool.h"
15  #include <stdlib.h>  #include <stdlib.h>
16  #include <string.h>  #include <string.h>
17    
# Line 25  MEMORY_POOL *memory_pool_init(size_t nod Line 21  MEMORY_POOL *memory_pool_init(size_t nod
21    
22          if (node_size < sizeof(void *))          if (node_size < sizeof(void *))
23          {          {
24                  log_error("Error: node_size < sizeof(void *)\n");                  log_error("Error: node_size < sizeof(void *)");
25                  return NULL;                  return NULL;
26          }          }
27    
28          p_pool = malloc(sizeof(MEMORY_POOL));          p_pool = malloc(sizeof(MEMORY_POOL));
29          if (p_pool == NULL)          if (p_pool == NULL)
30          {          {
31                  log_error("malloc(MEMORY_POOL) error: OOM\n");                  log_error("malloc(MEMORY_POOL) error: OOM");
32                  return NULL;                  return NULL;
33          }          }
34    
# Line 45  MEMORY_POOL *memory_pool_init(size_t nod Line 41  MEMORY_POOL *memory_pool_init(size_t nod
41          p_pool->p_chunks = malloc(sizeof(void *) * (size_t)chunk_count_limit);          p_pool->p_chunks = malloc(sizeof(void *) * (size_t)chunk_count_limit);
42          if (p_pool->p_chunks == NULL)          if (p_pool->p_chunks == NULL)
43          {          {
44                  log_error("malloc(sizeof(void *) * %d) error: OOM\n", chunk_count_limit);                  log_error("malloc(sizeof(void *) * %d) error: OOM", chunk_count_limit);
45                  free(p_pool);                  free(p_pool);
46                  return NULL;                  return NULL;
47          }          }
# Line 66  void memory_pool_cleanup(MEMORY_POOL *p_ Line 62  void memory_pool_cleanup(MEMORY_POOL *p_
62    
63          if (p_pool->node_count_allocated > 0)          if (p_pool->node_count_allocated > 0)
64          {          {
65                  log_error("Still have %d in-use nodes\n", p_pool->node_count_allocated);                  log_error("Still have %d in-use nodes", p_pool->node_count_allocated);
66          }          }
67    
68          while (p_pool->chunk_count > 0)          while (p_pool->chunk_count > 0)
# Line 84  inline static void *memory_pool_add_chun Line 80  inline static void *memory_pool_add_chun
80          void *p_chunk;          void *p_chunk;
81          void *p_node;          void *p_node;
82          size_t i;          size_t i;
83            size_t chunk_size;
84    
85          if (p_pool->chunk_count >= p_pool->chunk_count_limit)          if (p_pool->chunk_count >= p_pool->chunk_count_limit)
86          {          {
87                  log_error("Chunk count limit %d reached\n", p_pool->chunk_count);                  log_error("Chunk count limit %d reached", p_pool->chunk_count);
88                  return NULL;                  return NULL;
89          }          }
90          p_chunk = malloc(p_pool->node_size * p_pool->node_count_per_chunk);  
91            chunk_size = p_pool->node_size * p_pool->node_count_per_chunk;
92            p_chunk = malloc(chunk_size);
93          if (p_chunk == NULL)          if (p_chunk == NULL)
94          {          {
95                  log_error("malloc(%d * %d) error: OOM\n", p_pool->node_size, p_pool->node_count_per_chunk);                  log_error("malloc(%zu) error: OOM", chunk_size);
96                  return NULL;                  return NULL;
97          }          }
98    
99          p_node = p_pool->p_free;          p_node = p_pool->p_free;
100          memcpy(p_chunk + (p_pool->node_count_per_chunk - 1) * p_pool->node_size, &p_node, sizeof(p_node));          memcpy((char *)p_chunk + (p_pool->node_count_per_chunk - 1) * p_pool->node_size, &p_node, sizeof(p_node));
101          for (i = 0; i < p_pool->node_count_per_chunk - 1; i++)          for (i = 0; i < p_pool->node_count_per_chunk - 1; i++)
102          {          {
103                  p_node = p_chunk + (i + 1) * p_pool->node_size;                  p_node = (char *)p_chunk + (i + 1) * p_pool->node_size;
104                  memcpy(p_chunk + i * p_pool->node_size, &p_node, sizeof(p_node));                  memcpy((char *)p_chunk + i * p_pool->node_size, &p_node, sizeof(p_node));
105          }          }
106    
107          p_pool->p_chunks[p_pool->chunk_count] = p_chunk;          p_pool->p_chunks[p_pool->chunk_count] = p_chunk;
# Line 121  void *memory_pool_alloc(MEMORY_POOL *p_p Line 120  void *memory_pool_alloc(MEMORY_POOL *p_p
120    
121          if (p_pool == NULL)          if (p_pool == NULL)
122          {          {
123                  log_error("NULL pointer error\n");                  log_error("NULL pointer error");
124                  return NULL;                  return NULL;
125          }          }
126    
127          if (p_pool->p_free == NULL && memory_pool_add_chunk(p_pool) == NULL)          if (p_pool->p_free == NULL && memory_pool_add_chunk(p_pool) == NULL)
128          {          {
129                  log_error("Add chunk error\n");                  log_error("Add chunk error");
130                  return NULL;                  return NULL;
131          }          }
132    
# Line 144  void memory_pool_free(MEMORY_POOL *p_poo Line 143  void memory_pool_free(MEMORY_POOL *p_poo
143  {  {
144          if (p_pool == NULL)          if (p_pool == NULL)
145          {          {
146                  log_error("NULL pointer error\n");                  log_error("NULL pointer error");
147                  return;                  return;
148          }          }
149    
150          // For test and debug          // For test and debug
151    #ifdef _DEBUG
152          memory_pool_check_node(p_pool, p_node);          memory_pool_check_node(p_pool, p_node);
153    #endif
154    
155          memcpy(p_node, &(p_pool->p_free), sizeof(p_pool->p_free));          memcpy(p_node, &(p_pool->p_free), sizeof(p_pool->p_free));
156          p_pool->p_free = p_node;          p_pool->p_free = p_node;
# Line 165  int memory_pool_check_node(MEMORY_POOL * Line 166  int memory_pool_check_node(MEMORY_POOL *
166    
167          if (p_pool == NULL || p_node == NULL)          if (p_pool == NULL || p_node == NULL)
168          {          {
169                  log_error("NULL pointer error\n");                  log_error("NULL pointer error");
170                  return -1;                  return -1;
171          }          }
172    
# Line 173  int memory_pool_check_node(MEMORY_POOL * Line 174  int memory_pool_check_node(MEMORY_POOL *
174    
175          for (i = 0; i < p_pool->chunk_count; i++)          for (i = 0; i < p_pool->chunk_count; i++)
176          {          {
177                  if (p_node >= p_pool->p_chunks[i] && p_node < p_pool->p_chunks[i] + chunk_size)                  if (p_node >= p_pool->p_chunks[i] && (char *)p_node < (char *)(p_pool->p_chunks[i]) + chunk_size)
178                  {                  {
179                          if ((size_t)(p_node - p_pool->p_chunks[i]) % p_pool->node_size == 0)                          if ((size_t)((char *)p_node - (char *)(p_pool->p_chunks[i])) % p_pool->node_size == 0)
180                          {                          {
181                                  return 0; // OK                                  return 0; // OK
182                          }                          }
183                          else                          else
184                          {                          {
185                                  log_error("Address of node (%p) is not aligned with border of chunk %d [%p, %p)\n",                                  log_error("Address of node (%p) is not aligned with border of chunk %d [%p, %p)",
186                                                    i, p_node >= p_pool->p_chunks[i], p_pool->p_chunks[i] + chunk_size);                                                    p_node, i, p_pool->p_chunks[i], (char *)(p_pool->p_chunks[i]) + chunk_size);
187                                  return -3;                                  return -3;
188                          }                          }
189                  }                  }
190          }          }
191    
192          log_error("Address of node is not in range of chunks\n");          log_error("Address of node is not in range of chunks");
193          return -2;          return -2;
194  }  }


Legend:
Removed lines/characters  
Changed lines/characters
  Added lines/characters

webmaster@leafok.com
ViewVC Help
Powered by ViewVC 1.3.0-beta1