[go: up one dir, main page]

Menu

[bde3dc]: / util / nedit_malloc.c  Maximize  Restore  History

Download this file

91 lines (79 with data), 3.0 kB

 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
/*******************************************************************************
* *
* nedit_malloc.c -- Nirvana Editor memory handling *
* *
* Copyright (C) 2015 Ivan Skytte Joergensen *
* *
* This 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. In addition, you may distribute version of this program linked to *
* Motif or Open Motif. See README for details. *
* *
* This software 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 *
* software; if not, write to the Free Software Foundation, Inc., 59 Temple *
* Place, Suite 330, Boston, MA 02111-1307 USA *
* *
* Nirvana Text Editor *
* April, 1997 *
* *
* Written by Mark Edel *
* *
*******************************************************************************/
#include "nedit_malloc.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
/* The reason for these routines are that we have a single place to handle
* out-of-memory conditions. Also memory returned by Xt* is freed with NEditFree
* (investigation back to X11R4 reveals that XtMalloc ultimately called plain
* malloc(). Also, XtMalloc() and friends take a 'Cardinal' size which is
* always 32-bit preventing 4GB+ file support
*/
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;
}