1/*
2 * This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 *
6 * Copyright 1997 - July 2008 CWI, August 2008 - 2019 MonetDB B.V.
7 */
8
9#ifndef _SQL_MEM_H_
10#define _SQL_MEM_H_
11
12#include "gdk.h"
13
14#define SQL_OK 1
15#define SQL_ERR 0
16
17#ifdef WIN32
18#if defined(LIBSQLSERVER) || defined(LIBSQLCOMMON) || defined(LIBBATSTORE) || defined(LIBSTORE) || defined(LIBSQL)
19#define sql_export extern __declspec(dllexport)
20#define sqlcommon_export extern __declspec(dllexport)
21#define sqlbat_export extern __declspec(dllexport)
22#define sqlstore_export extern __declspec(dllexport)
23#else
24#define sql_export extern __declspec(dllimport)
25#define sqlcommon_export extern __declspec(dllimport)
26#define sqlbat_export extern __declspec(dllimport)
27#define sqlstore_export extern __declspec(dllimport)
28#endif
29#else
30#define sql_export extern
31#define sqlcommon_export extern
32#define sqlbat_export extern
33#define sqlstore_export extern
34#endif
35
36#define MNEW( type ) (type*)GDKmalloc(sizeof(type) )
37#define ZNEW( type ) (type*)GDKzalloc(sizeof(type) )
38#define NEW_ARRAY( type, size ) (type*)GDKmalloc((size)*sizeof(type))
39#define RENEW_ARRAY( type,ptr,size) (type*)GDKrealloc((void*)ptr,(size)*sizeof(type))
40
41#define NEWADT( size ) (adt*)GDKmalloc(size)
42#define _DELETE( ptr ) do { GDKfree(ptr); ptr = NULL; } while (0)
43#define _STRDUP( ptr ) GDKstrdup((char*)ptr)
44
45extern void c_delete( const void *p );
46
47typedef struct sql_ref {
48 int refcnt;
49} sql_ref;
50
51extern sql_ref *sql_ref_init(sql_ref *r);
52extern int sql_ref_inc(sql_ref *r);
53extern int sql_ref_dec(sql_ref *r);
54
55typedef struct sql_allocator {
56 size_t size;
57 size_t nr;
58 char **blks;
59 size_t used; /* memory used in last block */
60 size_t usedmem; /* used memory */
61} sql_allocator;
62
63extern sql_allocator *sa_create(void);
64extern sql_allocator *sa_reset( sql_allocator *sa );
65extern void *sa_alloc( sql_allocator *sa, size_t sz );
66extern void *sa_zalloc( sql_allocator *sa, size_t sz );
67extern void *sa_realloc( sql_allocator *sa, void *ptr, size_t sz, size_t osz );
68extern void sa_destroy( sql_allocator *sa );
69extern char *sa_strndup( sql_allocator *sa, const char *s, size_t l);
70extern char *sa_strdup( sql_allocator *sa, const char *s);
71extern char *sa_strconcat( sql_allocator *sa, const char *s1, const char *s2);
72extern size_t sa_size( sql_allocator *sa );
73
74#define SA_NEW( sa, type ) ((type*)sa_alloc( sa, sizeof(type)) )
75#define SA_ZNEW( sa, type ) ((type*)sa_zalloc( sa, sizeof(type)) )
76#define SA_NEW_ARRAY( sa, type, size ) (type*)sa_alloc( sa, ((size)*sizeof(type)))
77#define SA_ZNEW_ARRAY( sa, type, size ) (type*)sa_zalloc( sa, ((size)*sizeof(type)))
78#define SA_RENEW_ARRAY( sa, type, ptr, sz, osz ) (type*)sa_realloc( sa, ptr, ((sz)*sizeof(type)), ((osz)*sizeof(type)))
79
80#define _strlen(s) (int)strlen(s)
81
82#if !defined(NDEBUG) && !defined(STATIC_CODE_ANALYSIS) && defined(__GNUC__)
83#define sa_alloc(sa, sz) \
84 ({ \
85 sql_allocator *_sa = (sa); \
86 size_t _sz = (sz); \
87 void *_res = sa_alloc(_sa, _sz); \
88 ALLOCDEBUG \
89 fprintf(stderr, \
90 "#sa_alloc(%p,%zu) -> %p" \
91 " %s[%s:%d]\n", \
92 _sa, _sz, _res, \
93 __func__, __FILE__, __LINE__); \
94 _res; \
95 })
96#define sa_zalloc(sa, sz) \
97 ({ \
98 sql_allocator *_sa = (sa); \
99 size_t _sz = (sz); \
100 void *_res = sa_zalloc(_sa, _sz); \
101 ALLOCDEBUG \
102 fprintf(stderr, \
103 "#sa_zalloc(%p,%zu) -> %p" \
104 " %s[%s:%d]\n", \
105 _sa, _sz, _res, \
106 __func__, __FILE__, __LINE__); \
107 _res; \
108 })
109#define sa_realloc(sa, ptr, sz, osz) \
110 ({ \
111 sql_allocator *_sa = (sa); \
112 void *_ptr = (ptr); \
113 size_t _sz = (sz); \
114 size_t _osz = (osz); \
115 void *_res = sa_realloc(_sa, _ptr, _sz, _osz); \
116 ALLOCDEBUG \
117 fprintf(stderr, \
118 "#sa_realloc(%p,%p,%zu,%zu) -> %p" \
119 " %s[%s:%d]\n", \
120 _sa, _ptr, _sz, _osz, \
121 _res, \
122 __func__, __FILE__, __LINE__); \
123 _res; \
124 })
125#define sa_strdup(sa, s) \
126 ({ \
127 sql_allocator *_sa = (sa); \
128 const char *_s = (s); \
129 char *_res = sa_strdup(_sa, _s); \
130 ALLOCDEBUG \
131 fprintf(stderr, \
132 "#sa_strdup(%p,len=%zu) -> %p" \
133 " %s[%s:%d]\n", \
134 _sa, strlen(_s), _res, \
135 __func__, __FILE__, __LINE__); \
136 _res; \
137 })
138#define sa_strndup(sa, s, l) \
139 ({ \
140 sql_allocator *_sa = (sa); \
141 const char *_s = (s); \
142 size_t _l = (l); \
143 char *_res = sa_strndup(_sa, _s, _l); \
144 ALLOCDEBUG \
145 fprintf(stderr, \
146 "#sa_strndup(%p,len=%zu) -> %p" \
147 " %s[%s:%d]\n", \
148 _sa, _l, _res, \
149 __func__, __FILE__, __LINE__); \
150 _res; \
151 })
152#endif
153
154#endif /*_SQL_MEM_H_*/
155