1 #ifndef lint
RCSid()2 static char *RCSid() { return RCSid("$Id: alloc.c,v 1.17 2011/09/04 11:08:33 markisch Exp $"); }
3 #endif
4
5 /* GNUPLOT - alloc.c */
6
7 /*[
8 * Copyright 1986 - 1993, 1998, 2004 Thomas Williams, Colin Kelley
9 *
10 * Permission to use, copy, and distribute this software and its
11 * documentation for any purpose with or without fee is hereby granted,
12 * provided that the above copyright notice appear in all copies and
13 * that both that copyright notice and this permission notice appear
14 * in supporting documentation.
15 *
16 * Permission to modify the software is granted, but not the right to
17 * distribute the complete modified source code. Modifications are to
18 * be distributed as patches to the released version. Permission to
19 * distribute binaries produced by compiling modified sources is granted,
20 * provided you
21 * 1. distribute the corresponding source modifications from the
22 * released version in the form of a patch file along with the binaries,
23 * 2. add special version identification to distinguish your version
24 * in addition to the base release version number,
25 * 3. provide your name and address as the primary contact for the
26 * support of your modified version, and
27 * 4. retain our contact information in regard to use of the base
28 * software.
29 * Permission to distribute the released version of the source code along
30 * with corresponding source modifications in the form of a patch file is
31 * granted with same provisions 2 through 4 for binary distributions.
32 *
33 * This software is provided "as is" without express or implied warranty
34 * to the extent permitted by applicable law.
35 ]*/
36
37 /*
38 * AUTHORS
39 *
40 * Alexander Lehmann (collected functions from misc.c and binary.c)
41 *
42 */
43
44 #include "alloc.h"
45 #include "util.h" /* for int_error() */
46
47 #ifndef GP_FARMALLOC
48 # ifdef FARALLOC
49 # define GP_FARMALLOC(size) farmalloc ((size))
50 # define GP_FARREALLOC(p,size) farrealloc ((p), (size))
51 # else
52 # ifdef MALLOC_ZERO_RETURNS_ZERO
53 # define GP_FARMALLOC(size) malloc ((size_t)((size==0)?1:size))
54 # else
55 # define GP_FARMALLOC(size) malloc ((size_t)(size))
56 # endif
57 # define GP_FARREALLOC(p,size) realloc ((p), (size_t)(size))
58 # endif
59 #endif
60
61 /* gp_alloc:
62 * allocate memory
63 * This is a protected version of malloc. It causes an int_error
64 * if there is not enough memory. If message is NULL, we allow NULL return.
65 * Otherwise, we handle the error, using the message to create the int_error string.
66 * Note cp/sp_extend uses realloc, so it depends on this using malloc().
67 */
68
69 generic *
gp_alloc(size_t size,const char * message)70 gp_alloc(size_t size, const char *message)
71 {
72 char *p; /* the new allocation */
73
74 p = GP_FARMALLOC(size); /* try again */
75 if (p == NULL) {
76 /* really out of memory */
77 if (message != NULL) {
78 int_error(NO_CARET, "out of memory for %s", message);
79 /* NOTREACHED */
80 }
81 /* else we return NULL */
82 }
83
84 return (p);
85 }
86
87 /*
88 * note gp_realloc assumes that failed realloc calls leave the original mem
89 * block allocated. If this is not the case with any C compiler, a substitue
90 * realloc function has to be used.
91 */
92
93 generic *
gp_realloc(generic * p,size_t size,const char * message)94 gp_realloc(generic *p, size_t size, const char *message)
95 {
96 char *res; /* the new allocation */
97
98 /* realloc(NULL,x) is meant to do malloc(x), but doesn't always */
99 if (!p)
100 return gp_alloc(size, message);
101
102 res = GP_FARREALLOC(p, size);
103 if (res == (char *) NULL) {
104 if (message != NULL) {
105 int_error(NO_CARET, "out of memory for %s", message);
106 /* NOTREACHED */
107 }
108 /* else we return NULL */
109 }
110
111 return (res);
112 }
113
114
115 #ifdef FARALLOC
116 void
gpfree(generic * p)117 gpfree(generic *p)
118 {
119 #ifdef _Windows
120 HGLOBAL hGlobal = GlobalHandle(p);
121 GlobalUnlock(hGlobal);
122 GlobalFree(hGlobal);
123 #else
124 farfree(p);
125 #endif
126 }
127
128 #endif
129