summaryrefslogtreecommitdiff
path: root/support/dktools/dk3mem.c
blob: ee033ae176b2d6cda16354733d9122a17f07f81d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/*
Copyright (C) 2011-2020, Dirk Krause
SPDX-License-Identifier: BSD-3-Clause
*/

/*
	WARNING: This file was generated by the dkct program (see
	http://dktools.sourceforge.net/ for details).
	Changes you make here will be lost if dkct is run again!
	You should modify the original source and run dkct on it.
	Original source: dk3mem.ctr
*/

/**	@file dk3mem.c The dk3mem module.
*/


#line 166 "dk3mem.ctr"

#include "dk3all.h"






#line 172 "dk3mem.ctr"



void
dk3mem_res(void *mp, size_t sz)
/* {{{ */
{
  if((NULL != mp) && (0 != sz)) {
#if DK3_HAVE_MEMSET
    memset(mp, (int)'\0', sz);
#else
#if DK3_HAVE_BZERO
    bzero(mp, sz);
#else
    register char *ptr;
    register size_t i;
    ptr = (char *)mp; i = sz;
    while(i--) { *(ptr++) = '\0'; }
#endif
#endif
  }
}
/* }}} */



void
dk3mem_cpy(void *dp, void const *sp, size_t sz)
/* {{{ */
{
  if((NULL != dp) && (NULL != sp) && (0 != sz)) {
#if DK3_HAVE_MEMCPY
    memcpy(dp, sp, sz);
#else
#if DK3_HAVE_BCOPY
    bcopy(sp, dp, sz);
#else
    register char *mydp;
    register char const *mysp;
    register size_t i;
    mydp = (char *)dp;
    mysp = (char const *)sp;
    i = sz;
    while(i--) { *(mydp++) = *(mysp++); }
#endif
#endif
  }
}
/* }}} */



int
dk3mem_cmp(void const *s1, void const *s2, size_t sz)
/* {{{ */
{
  int back = 0;
  if(NULL != s1) {
    if(NULL != s2) {
#if DK3_HAVE_MEMCMP
        back = memcmp(s1, s2, sz);
#else
#if DK3_HAVE_BCMP
        back = bcmp(s1, s2, sz);
#else
        register unsigned char const *mys1;
        register unsigned char const *mys2;
	register size_t i;
	register int myback = 0;
        mys1 = (unsigned char const *)s1;
	mys2 = (unsigned char const *)s2;
        for(i = 0; ((i < sz) && (0 == myback)); i++) {
          if(*mys1 > *mys2) {
            myback = 1;
          } else {
            if(*mys1 < *mys2) {
              myback = -1;
            } else {
              mys1++; mys2++;
            }
          }
        }
	back = myback;
#endif
#endif
    } else { back = 1; }
  } else {
    if(s2) { back = -1; }
  }
  return back;
}
/* }}} */



void *
dk3mem_malloc_app(size_t sz, dk3_app_t *app)
/* {{{ */
{
  void		*back = NULL;
  char		c8buffer[64];	/* Buffer to show number of bytes. */
  dkChar	dkbuffer[64];	/* Buffer to show number of bytes. */
  

#line 275 "dk3mem.ctr"
  if(0 != sz) {
#if DK3_ON_WINDOWS && (defined(_MSC_VER)) && (_MSC_VER > 1100)
    

#line 278 "dk3mem.ctr"
    back = (void *)LocalAlloc((LMEM_FIXED | LMEM_ZEROINIT), sz);
#else
#if DK3_HAVE_MALLOC && DK3_HAVE_FREE
    

#line 282 "dk3mem.ctr"
    back = malloc(sz);
    

#line 284 "dk3mem.ctr"
#else
#error "The malloc and free functions are not available here!"
#endif
#endif
    if(NULL != back) {	

#line 289 "dk3mem.ctr"
      dk3mem_res(back, sz);
    } else {	

#line 291 "dk3mem.ctr"
      if(NULL != app) {	

#line 292 "dk3mem.ctr"
        /* ERROR: Failed to allocate n bytes! */
	if(0 != dk3ma_um_to_c8_string(c8buffer,sizeof(c8buffer),(dk3_um_t)sz)) {
	  (void)dk3str_cnv_c8_to_str_app(
	    dkbuffer, DK3_SIZEOF(dkbuffer,dkChar), c8buffer, NULL
	  );
	  dk3app_log_i3(app, DK3_LL_ERROR, 12, 13, dkbuffer);
	} else {
	  dk3app_log_i1(app, DK3_LL_ERROR, 14);
	}
      }
    }
  } 

#line 304 "dk3mem.ctr"
  return back;
}
/* }}} */


void *
dk3mem_malloc(size_t sz)
/* {{{ */
{
  void *back;
  back = dk3mem_malloc_app(sz, NULL);
  return back;
}
/* }}} */



size_t
dk3mem_mul_size_t(size_t sz, size_t ne, int *ec)
/* {{{ */
{
  size_t back = 0;
  if (0 < sz) {
    if ((DK3_SIZE_T_MAX / sz) < ne) {
      if (NULL != ec) { *ec = DK3_ERROR_MATH_OVERFLOW; }
    } else {
      back = (sz * ne);
    }
  } else {
    if (NULL != ec) { *ec = DK3_ERROR_MATH_OUT_OF_RANGE; }
  }
  return back;
}
/* }}} */



size_t
dk3mem_add_size_t(size_t a, size_t b, int *ec)
{
  size_t back = 0;
  if((DK3_SIZE_T_MAX - a) >= b) { back = a + b; }
  else { if(NULL != ec) { *ec = DK3_ERROR_MATH_OVERFLOW; } }
  return back;
}


void *
dk3mem_alloc_app(size_t sz, size_t ne, dk3_app_t *app)
/* {{{ */
{
  void			*back = NULL;
  size_t		as = 0;		/* Allocation size. */
  char			c8b1[64];	/* Buffer for element size. */
  char			c8b2[64];	/* Buffer for number of elements. */
  dkChar		dkb1[64];	/* Buffer for element size. */
  dkChar		dkb2[64];	/* Buffer for number of elements. */
  int			ok = 0;		/* Flag: Success. */
  

#line 363 "dk3mem.ctr"
  if((0 != sz) && (0 != ne)) {	
    as = dk3mem_mul_size_t(sz, ne, NULL);
    if(0 != as) {
      back = dk3mem_malloc_app(as, app);
    } else {
      if(NULL != app) {
        /* ERROR: Numeric overflow! */
	ok = 0;
	dkb1[0] = dkb2[0] = dkT('\0');
	if(0 != dk3ma_um_to_c8_string(c8b1, sizeof(c8b1),(dk3_um_t)sz)) {
	  if(0 != dk3ma_um_to_c8_string(c8b2, sizeof(c8b2),(dk3_um_t)ne)) {
	    (void)dk3str_cnv_c8_to_str_app(
	      dkb1, DK3_SIZEOF(dkb1,dkChar), c8b1, NULL
	    );
	    (void)dk3str_cnv_c8_to_str_app(
	      dkb2, DK3_SIZEOF(dkb2,dkChar), c8b2, NULL
	    );
	    ok = 1;
	  }
	}
	if(0 != ok) {
	  dk3app_log_i5(app, DK3_LL_ERROR, 16, 17, 18, dkb1, dkb2);
	} else {
	  dk3app_log_i1(app, DK3_LL_ERROR, 15);
	}
      }
    }
  } 

#line 391 "dk3mem.ctr"
  return back;
}
/* }}} */



void *
dk3mem_alloc(size_t sz, size_t ne)
/* {{{ */
{
  void *back;
  back = dk3mem_alloc_app(sz, ne, NULL);
  return back;
}
/* }}} */



void
dk3mem_free (void *po)
/* {{{ */
{
  if(NULL != po) {
#if DK3_ON_WINDOWS && (defined(_MSC_VER)) && (_MSC_VER > 1100)
    LocalFree((HLOCAL)po);
#else
#if DK3_HAVE_MALLOC && DK3_HAVE_FREE
    free(po);
#endif
#endif
  }
}
/* }}} */

int
dk3mem_reset_secure(void	*mp, size_t sz) 
{
#if !DK3_ON_WINDOWS
  char		*ptr;
#endif
  int		 back = 0;
  if ((NULL != mp) && (0 < sz)) {
#if DK3_ON_WINDOWS
    SecureZeroMemory(mp, sz);
    back = 1;
#else
    dk3mem_res(mp, sz);
    back = 1;
    ptr  = (char *)mp;
    while (sz--) {
      if ('\0' != *(ptr++)) {
        back = 0;
      }
    }
#endif
  }
  return back;
}


/* vim: set ai sw=2 filetype=c foldmethod=marker foldopen=all : */