Debian Codegraph @

Unicode/memory.cpp

raw
1 /* Copyright (C) 2000-2012 by George Williams */
2 /*
3 * Redistribution and use in source and binary forms, with or without
4 * modification, are permitted provided that the following conditions are met:
5
6 * Redistributions of source code must retain the above copyright notice, this
7 * list of conditions and the following disclaimer.
8
9 * Redistributions in binary form must reproduce the above copyright notice,
10 * this list of conditions and the following disclaimer in the documentation
11 * and/or other materials provided with the distribution.
12
13 * The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #include <fontforge-config.h>
29
30 #include "ustring.h"
31
32 #include <stdarg.h>
33 #include <stdio.h>
34 #include <string.h>
35
36
37 void NoMoreMemMessage(void) {
38 /* Output an 'Out of memory' message, then continue */
39 fprintf(stderr, "Out of memory\n" );
40 }
41
42 void ExpandBuffer(void** p_buf, size_t elem_size, size_t increment,
43 int* p_maxalloc) {
44 *p_buf = realloc(*p_buf, (*p_maxalloc + increment) * elem_size);
45 memset((char*)*p_buf + (*p_maxalloc) * elem_size, 0, increment * elem_size);
46 *p_maxalloc += increment;
47 }
48
49 char *copy(const char *str) {
50 return str ? strdup(str) : NULL;
51 }
52
53 char *copyn(const char *str,long n) {
54 /**
55 * MIQ: Note that there is at least one site that relies on
56 * copyn copying up to n bytes including embedded nulls.
57 * So using strndup() doesn't provide the same outcomes
58 * to that code.
59 * https://github.com/fontforge/fontforge/issues/1239
60 */
61 char *ret;
62 if ( str==NULL )
63 return( NULL );
64
65 ret = (char *) malloc(n+1);
66 memcpy(ret,str,n);
67 ret[n]='\0';
68 return( ret );
69 }
70
71 /**
72 * Format a string, allocating a buffer as necessary.
73 * If memory allocation fails, the application is aborted.
74 * \param [in] fmt The printf-like format string
75 * \param [in] args Input arguments to the format string
76 * \return The buffer containing the formatted string.
77 * Must be freed by the caller.
78 */
79 char *vsmprintf(const char *fmt, va_list args) {
80 va_list args2;
81 char* ret;
82 int len;
83
84 va_copy(args2, args);
85 len = vsnprintf(NULL, 0, fmt, args2);
86 va_end(args2);
87
88 if (len < 0) {
89 return NULL;
90 }
91
92 ret = (char*)malloc(len + 1);
93 if (ret == NULL) {
94 perror("smprintf");
95 abort();
96 return NULL;
97 }
98
99 len = vsnprintf(ret, len + 1, fmt, args);
100
101 if (len < 0) {
102 free(ret);
103 return NULL;
104 }
105
106 return ret;
107 }
108
109 /**
110 * Format a string, allocating a buffer as necessary.
111 * See vsmprintf for more details.
112 */
113 char *smprintf(const char* fmt, ...) {
114 va_list args;
115 char *ret;
116
117 va_start(args, fmt);
118 ret = vsmprintf(fmt, args);
119 va_end(args);
120
121 return ret;
122 }
123