GRASS GIS 8 Programmer's Manual  8.5.0dev(2024)-bea8435a9e
db/dbmi_base/alloc.c
Go to the documentation of this file.
1 /*!
2  \file lib/db/dbmi_base/alloc.c
3 
4  \brief DBMI Library (base) - allocate memory
5 
6  (C) 1999-2009, 2011 by the GRASS Development Team
7 
8  This program is free software under the GNU General Public License
9  (>=v2). Read the file COPYING that comes with GRASS for details.
10 
11  \author Joel Jones (CERL/UIUC), Radim Blazek
12  \author Doxygenized by Martin Landa <landa.martin gmail.com> (2011)
13  */
14 
15 #include <string.h>
16 #include <stdlib.h>
17 #include <grass/dbmi.h>
18 
19 /*!
20  \brief Make a copy of string buffer
21 
22  Allocated string buffer should be freed by db_free().
23 
24  \param s source string buffer
25 
26  \return allocated string buffer
27  */
28 char *db_store(const char *s)
29 {
30  char *a;
31 
32  a = db_malloc(strlen(s) + 1);
33  if (a)
34  strcpy(a, s);
35  return a;
36 }
37 
38 /*!
39  \brief Allocate memory
40 
41  On failure is called db_memory_error().
42 
43  \param n number of bytes to be allocated
44 
45  \return pointer to allocated memory
46  */
47 void *db_malloc(int n)
48 {
49  void *s;
50 
51  if (n <= 0)
52  n = 1;
53  s = malloc((unsigned int)n);
54  if (s == NULL)
56  return s;
57 }
58 
59 /*!
60  \brief Allocate memory
61 
62  On failure is called db_memory_error().
63 
64  \param n number of entities
65  \param m entity size
66 
67  \return pointer to allocated memory
68  */
69 void *db_calloc(int n, int m)
70 {
71  void *s;
72 
73  if (n <= 0)
74  n = 1;
75  if (m <= 0)
76  m = 1;
77  s = calloc((unsigned int)n, (unsigned int)m);
78  if (s == NULL)
80  return s;
81 }
82 
83 /*!
84  \brief Reallocate memory
85 
86  On failure is called db_memory_error().
87 
88  \param s pointer to memory
89  \param n number of newly allocated bytes
90 
91  \return pointer to allocated memory
92  */
93 void *db_realloc(void *s, int n)
94 {
95  if (n <= 0)
96  n = 1;
97  if (s == NULL)
98  s = malloc((unsigned int)n);
99  else
100  s = realloc(s, (unsigned int)n);
101  if (s == NULL)
102  db_memory_error();
103  return s;
104 }
105 
106 /*!
107  \brief Free allocated memory
108 
109  \param s pointer to memory to be freed
110  */
111 void db_free(void *s)
112 {
113  free(s);
114 }
#define NULL
Definition: ccmath.h:32
void * db_calloc(int n, int m)
Allocate memory.
char * db_store(const char *s)
Make a copy of string buffer.
void db_free(void *s)
Free allocated memory.
void * db_malloc(int n)
Allocate memory.
void * db_realloc(void *s, int n)
Reallocate memory.
void db_memory_error(void)
Report memory error.
#define strcpy
Definition: parson.c:62
void * malloc(YYSIZE_T)
void free(void *)