make m_array header only

This commit is contained in:
Roman Fomin 2024-01-16 18:27:03 +07:00
parent 62cf2e2517
commit 5e1e614c63
3 changed files with 38 additions and 58 deletions

View File

@ -49,7 +49,7 @@ set(WOOF_SOURCES
i_video.c i_video.h i_video.c i_video.h
info.c info.h info.c info.h
m_argv.c m_argv.h m_argv.c m_argv.h
m_array.c m_array.h m_array.h
m_bbox.c m_bbox.h m_bbox.c m_bbox.h
m_cheat.c m_cheat.h m_cheat.c m_cheat.h
m_fixed.h m_fixed.h
@ -227,7 +227,7 @@ set(SETUP_SOURCES
i_system.c i_system.h i_system.c i_system.h
i_timer.c i_timer.h i_timer.c i_timer.h
m_argv.c m_argv.h m_argv.c m_argv.h
m_array.c m_array.h m_array.h
m_io.c m_io.h m_io.c m_io.h
m_misc2.c m_misc2.h m_misc2.c m_misc2.h
net_io.c net_io.h net_io.c net_io.h

View File

@ -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 <stddef.h>
#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;
}

View File

@ -19,6 +19,9 @@
// and any previously-taken pointers should be considered invalidated. // and any previously-taken pointers should be considered invalidated.
#include <stdlib.h> #include <stdlib.h>
#include <stddef.h>
#include "doomtype.h"
#ifndef M_ARRAY_INIT_CAPACITY #ifndef M_ARRAY_INIT_CAPACITY
#define M_ARRAY_INIT_CAPACITY 8 #define M_ARRAY_INIT_CAPACITY 8
@ -31,11 +34,20 @@ typedef struct
char buffer[]; char buffer[];
} m_array_buffer_t; } m_array_buffer_t;
m_array_buffer_t *array_ptr(void *v); inline static m_array_buffer_t *array_ptr(void *v)
int array_size(void *v); {
int array_capacity(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) \ #define array_grow(v, n) \
((v) = M_ArrayGrow((v), sizeof(*(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; \ (v) = NULL; \
} \ } \
} while (0) } 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;
}