Freeciv-3.2: /fast/freeciv/nightly/S3_2/utility/mem.c Source File

Freeciv-3.2

Loading...
Searching...
No Matches
mem.c
Go to the documentation of this file.
1/***********************************************************************
2 Freeciv - Copyright (C) 1996 - A Kjeldberg, L Gregersen, P Unold
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; either version 2, or (at your option)
6 any later version.
7
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12***********************************************************************/
13
14/**********************************************************************
15*
16* This module contains freeciv-specific memory management functions
17*
18**********************************************************************/
19
20#ifdef HAVE_CONFIG_H
21#include <fc_config.h>
22#endif
23
24#include <stdlib.h>
25#include <string.h>
26
27/* utility */
28#include "fcintl.h"
29#include "log.h"
30#include "shared.h" /* TRUE, FALSE */
31
32#include "mem.h"
33
34/******************************************************************/
38 static void handle_alloc_failure(size_t size, const char *called_as,
39 int line, const char *file)
40{
41 log_fatal("Out of memory trying to %s " SIZE_T_PRINTF " bytes at line %d of %s.",
42 called_as, size, line, file);
43
44 exit(EXIT_FAILURE);
45}
46
47#ifdef FREECIV_DEBUG
48/******************************************************************/
51static void sanity_check_size(size_t size, const char *called_as,
52 int line, const char *file)
53{
54 /* There used to be a sanity check here that would abort if more than
55 * 30 megabytes were allocated. Unfortunately this didn't work because
56 * savegame loading can potentially use a huge amount of memory for large
57 * games. Another problem is it doesn't help much because there's nothing
58 * preventing a large number of smaller allocations. */
59
60 if (size == 0) {
61 log_verbose("Warning: %s with size " SIZE_T_PRINTF " at line %d of %s",
62 called_as, size, line, file);
63 }
64}
65#endif /* FREECIV_DEBUG */
66
67/******************************************************************/
73 void *fc_real_malloc(size_t size,
74 const char *called_as, int line, const char *file)
75{
76 void *ptr;
77
78#ifdef FREECIV_DEBUG
79 sanity_check_size(size, called_as, line, file);
80#endif /* FREECIV_DEBUG */
81
82 /* Some systems return NULL on malloc(0)
83 * According to ANSI C, the return is implementation-specific,
84 * this is a safe guard. Having the extra byte is, of course, harmless. */
85#ifndef MALLOC_ZERO_OK
86 size = MAX(size, 1);
87#endif
88
89 ptr = malloc(size);
90 if (ptr == NULL) {
91 handle_alloc_failure(size, called_as, line, file);
92 }
93
94 return ptr;
95}
96
97/******************************************************************/
101 void *fc_real_realloc(void *ptr, size_t size,
102 const char *called_as, int line, const char *file)
103{
104 void *new_ptr;
105
106 if (!ptr) {
107 return fc_real_malloc(size, called_as, line, file);
108 }
109
110#ifdef FREECIV_DEBUG
111 sanity_check_size(size, called_as, line, file);
112#endif /* FREECIV_DEBUG */
113
114 new_ptr = realloc(ptr, size);
115 if (!new_ptr) {
116 handle_alloc_failure(size, called_as, line, file);
117 }
118
119 return new_ptr;
120}
121
122/******************************************************************/
132 void *fc_real_calloc(size_t nelem, size_t elsize,
133 const char *called_as, int line, const char *file)
134{
135 size_t size = nelem * elsize; /* Potential overflow */
136 void *ptr;
137
138 ptr = fc_real_malloc(size, called_as, line, file);
139 memset(ptr, 0, size);
140
141 return ptr;
142}
143
144/******************************************************************/
148 char *real_fc_strdup(const char *str,
149 const char *called_as, int line, const char *file)
150{
151 char *dest = fc_real_malloc(strlen(str) + 1, called_as, line, file);
152
153 /* No need to check whether dest is non-NULL! */
154 strcpy(dest, str);
155
156 return dest;
157}
str
#define str
Definition astring.c:76
incite_cost
char * incite_cost
Definition comments.c:74
fcintl.h
log.h
log_verbose
#define log_verbose(message,...)
Definition log.h:109
log_fatal
#define log_fatal(message,...)
Definition log.h:100
fc_real_calloc
void * fc_real_calloc(size_t nelem, size_t elsize, const char *called_as, int line, const char *file)
Definition mem.c:132
fc_real_malloc
void * fc_real_malloc(size_t size, const char *called_as, int line, const char *file)
Definition mem.c:73
handle_alloc_failure
static void handle_alloc_failure(size_t size, const char *called_as, int line, const char *file)
Definition mem.c:38
fc_real_realloc
void * fc_real_realloc(void *ptr, size_t size, const char *called_as, int line, const char *file)
Definition mem.c:101
real_fc_strdup
char * real_fc_strdup(const char *str, const char *called_as, int line, const char *file)
Definition mem.c:148
mem.h
shared.h
MAX
#define MAX(x, y)
Definition shared.h:54
size
size_t size
Definition specvec.h:72