summaryrefslogtreecommitdiff
path: root/support/dktools/dk4mema.ctr
blob: dad2c6eaf5d70008a9f432b55b3f9f641144bbab (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
%%	options

copyright owner	=	Dirk Krause
copyright year	=	2015-xxxx
license		=	bsd



%%	header

/**	@file
	Memory allocation with included
	diagnostics.
*/

#ifndef DK4CONF_H_INCLUDED
#include "dk4conf.h"
#endif

#ifndef DK4TYPES_H_INCLUDED
#include "dk4types.h"
#endif

#ifndef DK4APP_H_INCLUDED
#include "dk4app.h"
#endif



#ifdef __cplusplus
extern "C" {
#endif

/**	Allocate and initialize memory.
	@param	elsize	Element size.
	@param	nelem	Number of elements.
	@param	app	Application structure for diagnostics, may be NULL.
	@return	Pointer to new memory on success, NULL on error.
*/
void *
dk4mem_calloc_app(size_t elsize, size_t nelem, dk4_app_t *app);

#ifdef __cplusplus
}
#endif



/**	Allocate and initialize memory.
	@param	tp	Type to allocate.
	@param	ne	Number of elements to allocate.
	@param	app	Application structure for diagnostics, may be NULL.
	@return	Pointer to a new tp on success, NULL on error.
*/
#define	dk4mem_new_app(tp,ne,app) (tp *)dk4mem_calloc_app(sizeof(tp),ne,app)



%%	module

#include "dk4mema.h"
#include "dk4mem.h"
#include "dk4maodd.h"
#include "dk4const.h"


void *
dk4mem_calloc_app(size_t elsize, size_t nelem, dk4_app_t *app)
{
  dkChar	 elsb[64];
  dkChar	 nelb[64];
  void		*back	=	NULL;
  dk4_er_t	 er;
  int		 res;

  dk4error_init(&er);
  back = dk4mem_calloc(elsize, nelem, &er);
  if (NULL == back) {
    res = dk4ma_write_decimal_unsigned(
      elsb, DK4_SIZEOF(elsb,dkChar), (dk4_um_t)elsize, 0, NULL
    );
    if (0 != res) {
      res = dk4ma_write_decimal_unsigned(
        nelb, DK4_SIZEOF(nelb,dkChar), (dk4_um_t)nelem, 0, NULL
      );
    }
    switch (er.ec) {
      case DK4_E_MATH_OVERFLOW: {
        /* ERROR: Numeric overflow in size calculation */
	if (0 != res) {
	  dk4app_log_base5(app,DK4_LL_ERROR,83,84,85,nelb,elsb);
	} else {
	  dk4app_log_base1(app, DK4_LL_ERROR, 82);
	}
      } break;
      case DK4_E_INVALID_ARGUMENTS: {
        /* ERROR: Invalid sizes */
	if (0 != res) {
	  dk4app_log_base5(app,DK4_LL_ERROR,87,88,89,nelb,elsb);
	} else {
	  dk4app_log_base1(app, DK4_LL_ERROR, 86);
	}
      } break;
      default: {
        /* ERROR: Not enough memory */
	if (0 != res) {
	  dk4app_log_base5(app,DK4_LL_ERROR,91,92,93,nelb,elsb);
	} else {
	  dk4app_log_base1(app, DK4_LL_ERROR, 90);
	}
      } break;
    }
  }
  return back;
}