#include "nedit_malloc.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
void *NEditMalloc(
size_t size)
{
void *ptr = malloc(size);
if(!ptr) {
fprintf(stderr,
"NEditMalloc(%lu) failed\n", (
unsigned long)size);
exit(
1);
}
return ptr;
}
void *NEditCalloc(
size_t nmemb,
size_t size)
{
void *ptr = NEditMalloc(nmemb*size);
memset(ptr,
0,nmemb*size);
return ptr;
}
void *NEditRealloc(
void *ptr,
size_t new_size)
{
void *new_ptr = realloc(ptr,new_size);
if(!new_ptr && new_size) {
fprintf(stderr,
"NEditRealloc(%lu) failed\n", (
unsigned long)new_size);
exit(
1);
}
return new_ptr;
}
void NEditFree(
void *ptr)
{
free(ptr);
}
char *NEditStrdup(
const char *str)
{
size_t len;
if(!str)
return NULL;
len = strlen(str);
char *new_str= (
char*)malloc(len+
1);
if(!new_str) {
fprintf(stderr,
"NEditStrdup(%lu) failed\n", (
unsigned long)len);
exit(
1);
}
memcpy(new_str,str,len+
1);
return new_str;
}
char *NEditStrndup(
const char *str,
size_t len)
{
char *newstr = malloc(len+
1);
newstr[len] =
0;
memcpy(newstr, str, len);
return newstr;
}