From 45b6628e4a1d3bf3bea5fdca794af788ceabd053 Mon Sep 17 00:00:00 2001 From: Holger Schemel Date: Sat, 24 Feb 2024 14:03:58 +0100 Subject: [PATCH] renamed function to allocate and copy memory --- src/game_bd/bd_bdcff.c | 4 ++-- src/game_bd/bd_cave.c | 6 +++--- src/game_bd/bd_caveobject.c | 24 ++++++++++++------------ src/libgame/misc.c | 31 +++++++++++++------------------ src/libgame/misc.h | 3 +-- 5 files changed, 31 insertions(+), 37 deletions(-) diff --git a/src/game_bd/bd_bdcff.c b/src/game_bd/bd_bdcff.c index 1a204a2f..8f2ee081 100644 --- a/src/game_bd/bd_bdcff.c +++ b/src/game_bd/bd_bdcff.c @@ -1194,12 +1194,12 @@ boolean gd_caveset_load_from_bdcff(const char *contents) object.element = cave->initial_border; object.fill_element = cave->initial_border; - cave->objects = list_prepend(cave->objects, getMemCopy(&object, sizeof(object))); + cave->objects = list_prepend(cave->objects, get_memcpy(&object, sizeof(object))); object.x1 = 19; object.y1 = 0; /* 19, as it is also the border */ - cave->objects = list_prepend(cave->objects, getMemCopy(&object, sizeof(object))); /* another */ + cave->objects = list_prepend(cave->objects, get_memcpy(&object, sizeof(object))); /* another */ } } } diff --git a/src/game_bd/bd_cave.c b/src/game_bd/bd_cave.c index a1a5d409..9fa85405 100644 --- a/src/game_bd/bd_cave.c +++ b/src/game_bd/bd_cave.c @@ -506,7 +506,7 @@ gpointer gd_cave_map_dup_size(const GdCave *cave, const gpointer map, const int return NULL; rows = checked_malloc((cave->h) * sizeof(gpointer)); - rows[0] = getMemCopy (maplines[0], cell_size * cave->w * cave->h); + rows[0] = get_memcpy (maplines[0], cell_size * cave->w * cave->h); for (y = 1; y < cave->h; y++) rows[y] = (char *)rows[0] + cell_size * cave->w * y; @@ -605,7 +605,7 @@ void gd_cave_copy(GdCave *dest, const GdCave *src) dest->objects = NULL; /* new empty list */ for (iter = src->objects; iter != NULL; iter = iter->next) /* do a deep copy */ - dest->objects = list_append(dest->objects, getMemCopy (iter->data, sizeof (GdObject))); + dest->objects = list_append(dest->objects, get_memcpy (iter->data, sizeof (GdObject))); } /* copy replays */ @@ -1400,7 +1400,7 @@ GdReplay *gd_replay_new_from_replay(GdReplay *orig) { GdReplay *rep; - rep = getMemCopy(orig, sizeof(GdReplay)); + rep = get_memcpy(orig, sizeof(GdReplay)); /* replicate dynamic data */ rep->comment = getStringCopy(orig->comment); diff --git a/src/game_bd/bd_caveobject.c b/src/game_bd/bd_caveobject.c index d7617906..568d2b3a 100644 --- a/src/game_bd/bd_caveobject.c +++ b/src/game_bd/bd_caveobject.c @@ -313,7 +313,7 @@ GdObject *gd_object_new_from_string(char *str) { object.element = gd_get_element_from_string(elem0); - return getMemCopy(&object, sizeof (GdObject)); + return get_memcpy(&object, sizeof (GdObject)); } return NULL; @@ -327,7 +327,7 @@ GdObject *gd_object_new_from_string(char *str) { object.element = gd_get_element_from_string(elem0); - return getMemCopy(&object, sizeof (GdObject)); + return get_memcpy(&object, sizeof (GdObject)); } return NULL; @@ -341,7 +341,7 @@ GdObject *gd_object_new_from_string(char *str) object.type = GD_RECTANGLE; object.element = gd_get_element_from_string (elem0); - return getMemCopy(&object, sizeof (GdObject)); + return get_memcpy(&object, sizeof (GdObject)); } return NULL; @@ -360,14 +360,14 @@ GdObject *gd_object_new_from_string(char *str) object.element = gd_get_element_from_string (elem0); object.fill_element = gd_get_element_from_string (elem1); - return getMemCopy(&object, sizeof (GdObject)); + return get_memcpy(&object, sizeof (GdObject)); } if (paramcount == 5) { object.element = object.fill_element = gd_get_element_from_string (elem0); - return getMemCopy(&object, sizeof (GdObject)); + return get_memcpy(&object, sizeof (GdObject)); } return NULL; @@ -387,7 +387,7 @@ GdObject *gd_object_new_from_string(char *str) object.type = GD_RASTER; object.element = gd_get_element_from_string (elem0); - return getMemCopy(&object, sizeof (GdObject)); + return get_memcpy(&object, sizeof (GdObject)); } return NULL; @@ -403,7 +403,7 @@ GdObject *gd_object_new_from_string(char *str) object.element = gd_get_element_from_string (elem0); object.fill_element = gd_get_element_from_string (elem1); - return getMemCopy(&object, sizeof (GdObject)); + return get_memcpy(&object, sizeof (GdObject)); } return NULL; @@ -418,7 +418,7 @@ GdObject *gd_object_new_from_string(char *str) object.fill_element = gd_get_element_from_string (elem0); object.element = gd_get_element_from_string (elem1); - return getMemCopy(&object, sizeof (GdObject)); + return get_memcpy(&object, sizeof (GdObject)); } return NULL; @@ -433,7 +433,7 @@ GdObject *gd_object_new_from_string(char *str) object.fill_element = gd_get_element_from_string (elem0); object.element = gd_get_element_from_string (elem1); - return getMemCopy(&object, sizeof (GdObject)); + return get_memcpy(&object, sizeof (GdObject)); } return NULL; @@ -463,7 +463,7 @@ GdObject *gd_object_new_from_string(char *str) object.element = gd_get_element_from_string (elem0); object.fill_element = gd_get_element_from_string (elem1); - return getMemCopy(&object, sizeof (GdObject)); + return get_memcpy(&object, sizeof (GdObject)); } return NULL; @@ -519,7 +519,7 @@ GdObject *gd_object_new_from_string(char *str) if (l > 10 && l % 2 == 1) object.element = gd_get_element_from_string(words[l - 1]); - return getMemCopy(&object, sizeof (GdObject)); + return get_memcpy(&object, sizeof (GdObject)); } /* COPY PASTE OBJECT */ @@ -550,7 +550,7 @@ GdObject *gd_object_new_from_string(char *str) else Warn("invalid setting for copypaste flip property: %s", flip); - return getMemCopy(&object, sizeof(GdObject)); + return get_memcpy(&object, sizeof(GdObject)); } return NULL; diff --git a/src/libgame/misc.c b/src/libgame/misc.c index 7450c3a3..ed22f2c0 100644 --- a/src/libgame/misc.c +++ b/src/libgame/misc.c @@ -1084,24 +1084,6 @@ char *getBasePath(char *filename) } -// ---------------------------------------------------------------------------- -// various memory functions -// ---------------------------------------------------------------------------- - -void *getMemCopy(const void *m, size_t size) -{ - void *m_copy; - - if (m == NULL) - return NULL; - - m_copy = checked_malloc(size); - memcpy(m_copy, m, size); - - return m_copy; -} - - // ---------------------------------------------------------------------------- // various string functions // ---------------------------------------------------------------------------- @@ -1902,6 +1884,19 @@ void clear_mem(void *ptr, unsigned int size) #endif } +void *get_memcpy(const void *m, size_t size) +{ + void *m_copy; + + if (m == NULL) + return NULL; + + m_copy = checked_malloc(size); + memcpy(m_copy, m, size); + + return m_copy; +} + // ---------------------------------------------------------------------------- // various helper functions diff --git a/src/libgame/misc.h b/src/libgame/misc.h index 4548892c..1f207c14 100644 --- a/src/libgame/misc.h +++ b/src/libgame/misc.h @@ -168,8 +168,6 @@ char *getBaseName(char *); char *getBaseNamePtr(char *); char *getBaseNameNoSuffix(char *); -void *getMemCopy(const void *, size_t); - char *getStringCat2WithSeparator(const char *, const char *, const char *); char *getStringCat3WithSeparator(const char *, const char *, const char *, const char *); char *getStringCat2(const char *, const char *); @@ -212,6 +210,7 @@ void *checked_calloc(unsigned int); void *checked_realloc(void *, unsigned int); void checked_free(void *); void clear_mem(void *, unsigned int); +void *get_memcpy(const void *, size_t); void swap_numbers(int *, int *); void swap_number_pairs(int *, int *, int *, int *); -- 2.34.1