From 5e1e614c639dba467ebb0966ef466c4c1e469c53 Mon Sep 17 00:00:00 2001 From: Roman Fomin Date: Tue, 16 Jan 2024 18:27:03 +0700 Subject: [PATCH] make m_array header only --- src/CMakeLists.txt | 4 ++-- src/m_array.c | 52 ---------------------------------------------- src/m_array.h | 40 +++++++++++++++++++++++++++++++---- 3 files changed, 38 insertions(+), 58 deletions(-) delete mode 100644 src/m_array.c diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 0ac5ac75..6d97f5ed 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -49,7 +49,7 @@ set(WOOF_SOURCES i_video.c i_video.h info.c info.h m_argv.c m_argv.h - m_array.c m_array.h + m_array.h m_bbox.c m_bbox.h m_cheat.c m_cheat.h m_fixed.h @@ -227,7 +227,7 @@ set(SETUP_SOURCES i_system.c i_system.h i_timer.c i_timer.h m_argv.c m_argv.h - m_array.c m_array.h + m_array.h m_io.c m_io.h m_misc2.c m_misc2.h net_io.c net_io.h diff --git a/src/m_array.c b/src/m_array.c deleted file mode 100644 index daf36e91..00000000 --- a/src/m_array.c +++ /dev/null @@ -1,52 +0,0 @@ -// -// Copyright(C) 2024 Roman Fomin -// -// This program 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. -// -// This program 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. - -#include - -#include "m_array.h" -#include "i_system.h" - -m_array_buffer_t *array_ptr(void *v) -{ - return (m_array_buffer_t *)((char *)v - offsetof(m_array_buffer_t, buffer)); -} - -int array_size(void *v) -{ - return v ? array_ptr(v)->size : 0; -} - -int array_capacity(void *v) -{ - return v ? array_ptr(v)->capacity : 0; -} - -void *M_ArrayGrow(void *v, size_t esize, int n) -{ - m_array_buffer_t *p; - - if (v) - { - p = array_ptr(v); - p = I_Realloc(p, sizeof(m_array_buffer_t) + (p->capacity + n) * esize); - p->capacity += n; - } - else - { - p = malloc(sizeof(m_array_buffer_t) + n * esize); - p->capacity = n; - p->size = 0; - } - - return p->buffer; -} diff --git a/src/m_array.h b/src/m_array.h index a0250317..0380e125 100644 --- a/src/m_array.h +++ b/src/m_array.h @@ -19,6 +19,9 @@ // and any previously-taken pointers should be considered invalidated. #include +#include + +#include "doomtype.h" #ifndef M_ARRAY_INIT_CAPACITY #define M_ARRAY_INIT_CAPACITY 8 @@ -31,11 +34,20 @@ typedef struct char buffer[]; } m_array_buffer_t; -m_array_buffer_t *array_ptr(void *v); -int array_size(void *v); -int array_capacity(void *v); +inline static m_array_buffer_t *array_ptr(void *v) +{ + return (m_array_buffer_t *)((char *)v - offsetof(m_array_buffer_t, buffer)); +} -void *M_ArrayGrow(void *v, size_t esize, int n); +inline static int array_size(void *v) +{ + return v ? array_ptr(v)->size : 0; +} + +inline static int array_capacity(void *v) +{ + return v ? array_ptr(v)->capacity : 0; +} #define array_grow(v, n) \ ((v) = M_ArrayGrow((v), sizeof(*(v)), n)) @@ -63,3 +75,23 @@ void *M_ArrayGrow(void *v, size_t esize, int n); (v) = NULL; \ } \ } while (0) + +inline static void *M_ArrayGrow(void *v, size_t esize, int n) +{ + m_array_buffer_t *p; + + if (v) + { + p = array_ptr(v); + p = realloc(p, sizeof(m_array_buffer_t) + (p->capacity + n) * esize); + p->capacity += n; + } + else + { + p = malloc(sizeof(m_array_buffer_t) + n * esize); + p->capacity = n; + p->size = 0; + } + + return p->buffer; +}