Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add reusable, libc-compatible pkg/shm allocation functions
- Loading branch information
1 parent
549ddcb
commit 4dff90f
Showing
2 changed files
with
154 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
/* | ||
* OpenSIPS equivalent of the stdlib allocation functions | ||
* | ||
* Copyright (C) 2017 OpenSIPS Project | ||
* | ||
* This file is part of opensips, a free SIP server. | ||
* | ||
* opensips is free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation; either version 2 of the License, or | ||
* (at your option) any later version | ||
* | ||
* opensips is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program; if not, write to the Free Software | ||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
*/ | ||
|
||
#include <string.h> | ||
|
||
#include "../mem/mem.h" | ||
#include "../mem/shm_mem.h" | ||
|
||
#include "osips_malloc.h" | ||
|
||
/* PKG (fastest) */ | ||
|
||
void *osips_pkg_malloc(size_t size) | ||
{ | ||
return pkg_malloc(size); | ||
} | ||
|
||
void osips_pkg_free(void *ptr) | ||
{ | ||
pkg_free(ptr); | ||
} | ||
|
||
void *osips_pkg_calloc(size_t nmemb, size_t size) | ||
{ | ||
void *p; | ||
|
||
p = pkg_malloc(nmemb * size); | ||
if (p) { | ||
memset(p, '\0', nmemb * size); | ||
} | ||
|
||
return p; | ||
} | ||
|
||
void *osips_pkg_realloc(void *ptr, size_t size) | ||
{ | ||
return pkg_realloc(ptr, size); | ||
} | ||
|
||
char *osips_pkg_strdup(const char *s) | ||
{ | ||
char *rval; | ||
int len; | ||
|
||
len = strlen(s) + 1; | ||
rval = pkg_malloc(len); | ||
if (!rval) { | ||
return NULL; | ||
} | ||
|
||
memcpy(rval, s, len); | ||
return rval; | ||
} | ||
|
||
/* SHM */ | ||
|
||
void *osips_shm_malloc(size_t size) | ||
{ | ||
return shm_malloc(size); | ||
} | ||
|
||
void osips_shm_free(void *ptr) | ||
{ | ||
shm_free(ptr); | ||
} | ||
|
||
void *osips_shm_calloc(size_t nmemb, size_t size) | ||
{ | ||
void *p; | ||
|
||
p = shm_malloc(nmemb * size); | ||
if (p) { | ||
memset(p, '\0', nmemb * size); | ||
} | ||
|
||
return p; | ||
} | ||
|
||
void *osips_shm_realloc(void *ptr, size_t size) | ||
{ | ||
return shm_realloc(ptr, size); | ||
} | ||
|
||
char *osips_shm_strdup(const char *s) | ||
{ | ||
char *rval; | ||
int len; | ||
|
||
len = strlen(s) + 1; | ||
rval = shm_malloc(len); | ||
if (!rval) { | ||
return NULL; | ||
} | ||
|
||
memcpy(rval, s, len); | ||
return rval; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* OpenSIPS equivalent of the stdlib allocation functions | ||
* | ||
* Copyright (C) 2017 OpenSIPS Project | ||
* | ||
* This file is part of opensips, a free SIP server. | ||
* | ||
* opensips is free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation; either version 2 of the License, or | ||
* (at your option) any later version | ||
* | ||
* opensips is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program; if not, write to the Free Software | ||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
*/ | ||
|
||
#ifndef __OSIPS_MALLOC_H__ | ||
#define __OSIPS_MALLOC_H__ | ||
|
||
void *osips_pkg_malloc(size_t size); | ||
void osips_pkg_free(void *ptr); | ||
void *osips_pkg_calloc(size_t nmemb, size_t size); | ||
void *osips_pkg_realloc(void *ptr, size_t size); | ||
char *osips_pkg_strdup(const char *s); | ||
|
||
void *osips_shm_malloc(size_t size); | ||
void osips_shm_free(void *ptr); | ||
void *osips_shm_calloc(size_t nmemb, size_t size); | ||
void *osips_shm_realloc(void *ptr, size_t size); | ||
char *osips_shm_strdup(const char *s); | ||
|
||
#endif /* __OSIPS_MALLOC_H__ */ |