Skip to content

Commit f5269e0

Browse files
authored
Merge pull request #159 from Parallel-NetCDF/strdup
add NCI_Strdup
2 parents f64973f + 899386c commit f5269e0

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

src/drivers/common/mem_alloc.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,32 @@ void *NCI_Malloc_fn(size_t size,
202202
}
203203

204204

205+
/*----< NCI_Strdup() >-------------------------------------------------------*/
206+
/* This subroutine is esstentially the same as calling strdup().
207+
*/
208+
void *NCI_Strdup_fn(const char *src,
209+
const int lineno,
210+
const char *func,
211+
const char *filename)
212+
{
213+
if (src == NULL) return NULL;
214+
215+
size_t len = strlen(src);
216+
void *buf = malloc(len + 1);
217+
#ifdef PNETCDF_DEBUG
218+
if (len >= 0 && buf == NULL)
219+
fprintf(stderr, "malloc(%zd) failed in file %s func %s line %d\n",
220+
len, filename, func, lineno);
221+
#endif
222+
if (len > 0) memcpy(buf, src, len);
223+
((char*)buf)[len] = '\0';
224+
#ifdef PNC_MALLOC_TRACE
225+
ncmpii_add_mem_entry(buf, len, lineno, func, filename);
226+
#endif
227+
return buf;
228+
}
229+
230+
205231
/*----< NCI_Calloc_fn() >-----------------------------------------------------*/
206232
/* This subroutine is esstentially the same as calling calloc().
207233
* According to calloc man page, If nelem is 0, then calloc() returns either

src/drivers/include/common.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ extern void *
5757
NCI_Malloc_fn(size_t size, const int lineno, const char *func,
5858
const char *filename);
5959

60+
extern void *
61+
NCI_Strdup_fn(const char *src, const int lineno, const char *func,
62+
const char *filename);
63+
6064
extern void *
6165
NCI_Calloc_fn(size_t nelem, size_t elsize, const int lineno, const char *func,
6266
const char *filename);
@@ -71,11 +75,13 @@ NCI_Free_fn(void *ptr, const int lineno, const char *func,
7175

7276
#if defined(PNETCDF_DEBUG) || defined(PNC_MALLOC_TRACE)
7377
#define NCI_Malloc(a) NCI_Malloc_fn(a,__LINE__,__func__,__FILE__)
78+
#define NCI_Strdup(a) NCI_Strdup_fn(a,__LINE__,__func__,__FILE__)
7479
#define NCI_Calloc(a,b) NCI_Calloc_fn(a,b,__LINE__,__func__,__FILE__)
7580
#define NCI_Realloc(a,b) NCI_Realloc_fn(a,b,__LINE__,__func__,__FILE__)
7681
#define NCI_Free(a) NCI_Free_fn(a,__LINE__,__func__,__FILE__)
7782
#else
7883
#define NCI_Malloc(a) malloc(a)
84+
#define NCI_Strdup(a) strdup(a)
7985
#define NCI_Calloc(a,b) calloc(a,b)
8086
#define NCI_Realloc(a,b) realloc(a,b)
8187
#define NCI_Free(a) free(a)

0 commit comments

Comments
 (0)