GRASS 8 Programmer's Manual 8.6.0dev(2026)-4bb960b182
Loading...
Searching...
No Matches
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 SPDX-FileCopyrightText: 1999-2009, 2011 GRASS Development Team
7 SPDX-License-Identifier: GPL-2.0-or-later
8
9 \author Joel Jones (CERL/UIUC), Radim Blazek
10 \author Doxygenized by Martin Landa <landa.martin gmail.com> (2011)
11 */
12
13#include <string.h>
14#include <stdlib.h>
15#include <grass/dbmi.h>
16
17/*!
18 \brief Make a copy of string buffer
19
20 Allocated string buffer should be freed by db_free().
21
22 \param s source string buffer
23
24 \return allocated string buffer
25 */
26char *db_store(const char *s)
27{
28 char *a;
29
30 a = db_malloc(strlen(s) + 1);
31 if (a)
32 strcpy(a, s);
33 return a;
34}
35
36/*!
37 \brief Allocate memory
38
39 On failure is called db_memory_error().
40
41 \param n number of bytes to be allocated
42
43 \return pointer to allocated memory
44 */
45void *db_malloc(int n)
46{
47 void *s;
48
49 if (n <= 0)
50 n = 1;
51 s = malloc((unsigned int)n);
52 if (s == NULL)
54 return s;
55}
56
57/*!
58 \brief Allocate memory
59
60 On failure is called db_memory_error().
61
62 \param n number of entities
63 \param m entity size
64
65 \return pointer to allocated memory
66 */
67void *db_calloc(int n, int m)
68{
69 void *s;
70
71 if (n <= 0)
72 n = 1;
73 if (m <= 0)
74 m = 1;
75 s = calloc((unsigned int)n, (unsigned int)m);
76 if (s == NULL)
78 return s;
79}
80
81/*!
82 \brief Reallocate memory
83
84 On failure is called db_memory_error().
85
86 \param s pointer to memory
87 \param n number of newly allocated bytes
88
89 \return pointer to allocated memory
90 */
91void *db_realloc(void *s, int n)
92{
93 if (n <= 0)
94 n = 1;
95 if (s == NULL)
96 s = malloc((unsigned int)n);
97 else
98 s = realloc(s, (unsigned int)n);
99 if (s == NULL)
101 return s;
102}
103
104/*!
105 \brief Free allocated memory
106
107 \param s pointer to memory to be freed
108 */
109void db_free(void *s)
110{
111 free(s);
112}
#define NULL
Definition ccmath.h:32
char * db_store(const char *s)
Make a copy of string buffer.
void * db_malloc(int n)
Allocate memory.
void * db_calloc(int n, int m)
Allocate memory.
void db_free(void *s)
Free allocated memory.
void * db_realloc(void *s, int n)
Reallocate memory.
Main header of GRASS DataBase Management Interface.
void db_memory_error(void)
Report memory error.
#define strcpy
Definition parson.c:66
void * malloc(unsigned)
void free(void *)