librnr  1.14.5
RoadNarrows Robotics Common Library 1
new.h
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////////
2 /*! \file
3  *
4  * \brief Memory allocation and deallocation declarations.
5  *
6  * General purpose dynamic memory allocate and deallocate declarations and
7  * defines. None of the calls will fail, unless fatally for the entire
8  * program. Modeled on C++ new() and delete() operators.
9  *
10  * \pkgsynopsis
11  * RoadNarrows Robotics Common Library 1
12  *
13  * \pkgcomponent{Library}
14  * librnr
15  *
16  * \pkgfile{rnr/new.h}
17  *
18  * \author Robin Knight (robin.knight@roadnarrows.com)
19  *
20  * \pkgcopyright{2005-2018,RoadNarrows LLC.,http://www.roadnarrows.com}
21  *
22  * \license{MIT}
23  *
24  * \EulaBegin
25  * See the README and EULA files for any copyright and licensing information.
26  * \EulaEnd
27  */
28 ////////////////////////////////////////////////////////////////////////////////
29 
30 #ifndef _RNR_NEW_H
31 #define _RNR_NEW_H
32 
33 #include "rnr/rnrconfig.h"
34 
35 #include <sys/types.h>
36 #include <stdlib.h>
37 #include <stdarg.h>
38 
39 /*!
40  * \brief Allocate new type.
41  *
42  * \param T Data type.
43  *
44  * \return Pointer T* to allocated memory
45  *
46  * \exception "Bad Allocation"
47  * Program is terminated with exit code \ref EC_ERROR.
48  */
49 #define NEW(T) (T *)new(sizeof(T))
50 
51 //
52 // Allocate new string
53 //
54 /*!
55  * \brief Allocate new string buffer of length len+1.
56  *
57  * \param len Length of string buffer less null-termination character.
58  *
59  * \return Returns char * to allocated, zero'ed out memory.
60  *
61  * \exception "Bad Allocation"
62  * Program is terminated with exit code \ref EC_ERROR.
63  */
64 #define NEWSTR(len) (char *)new( sizeof(char)*((size_t)((len)+1)) )
65 
67 
68 #ifndef __cplusplus
69 
70 /*!
71  * \brief delete() "operator"
72  *
73  * Frees previously allocated memory. NULL pointer produce no action.
74  *
75  * \param p Pointer to memory.
76  */
77 INLINE_IN_H void delete(void *p)
78 {
79  if( p != NULL )
80  {
81  free(p);
82  }
83 }
84 
85 extern void *new(size_t size);
86 
87 #endif // __cplusplus
88 
89 extern void *new_overs(void *pSrc, size_t sizeDup, size_t sizeNew);
90 
91 extern void *new_memdup(size_t size, void *data);
92 
93 extern char *new_strdup(const char *s);
94 
95 extern char *new_strndup(const char *s, size_t n);
96 
98 
99 //
100 // Common platform utilities
101 //
102 #if !defined(__windows__)
103 
105 
106 extern char *strcpy_s(char *dest, size_t n, const char *src);
107 
109 
110 #ifdef __cplusplus
111 /*!
112  * \brief Format print to string up to sizeof(str)-1 characters. String is
113  * guaranteed to be null terminated.
114  *
115  * \param str Destination string buffer.
116  * \param format Standard printf formatting string.
117  * \param ... Variable argument list.
118  *
119  * \return Number of characters printed.
120  */
121 template <size_t size>
122 inline int sprintf_s(char (&str)[size], const char *format, ...)
123 {
124  va_list ap;
125  size_t n = sizeof(str);
126  int nPrinted;
127 
128  va_start(ap, format);
129  nPrinted = vsnprintf(str, n, format, ap);
130  va_end(ap);
131  str[n-1] = 0;
132  return nPrinted;
133 }
134 
135 #else
136 
138 
139 extern int sprintf_s(char *str, size_t n, const char *format, ...);
140 
142 
143 #endif // __cplusplus
144 
145 #endif // ! __windows__
146 
147 
148 #endif // _RNR_NEW_H
char * new_strndup(const char *s, size_t n)
Duplicate not more than n characters of string.
Definition: new.c:208
char * new_strdup(const char *s)
Duplicate a string.
Definition: new.c:176
#define NULL
null pointer
Definition: rnrconfig.h:199
void * new_memdup(size_t size, void *data)
Duplicate data.
Definition: new.c:146
#define C_DECLS_BEGIN
C declaration block begin in C.
Definition: rnrconfig.h:71
int sprintf_s(char *str, size_t n, const char *format,...)
Format print to string up to n-1 characters. String is guaranteed to be null terminated.
Definition: win.c:83
void * new_overs(void *pSrc, size_t sizeDup, size_t sizeNew)
Allocate and duplicate.
Definition: new.c:112
RoadNarrows Robotics common configuration file.
#define INLINE_IN_H
inline C funtion in C header
Definition: rnrconfig.h:157
#define C_DECLS_END
C declaration block end in C.
Definition: rnrconfig.h:72
char * strcpy_s(char *dest, size_t n, const char *src)
Copy source string to destinations string. At most n bytes will be copied. The destination string is ...
Definition: win.c:65