diff --git a/src/Sourcefile b/src/Sourcefile index e7a77c82e..35f394dd8 100644 --- a/src/Sourcefile +++ b/src/Sourcefile @@ -46,7 +46,7 @@ tables.c v_video.c w_wad.c y_inter.c -z_zone.c +z_zone.cpp r_bsp.c r_data.c r_draw.c diff --git a/src/core/memory.cpp b/src/core/memory.cpp index e75cf91f7..d10ff7eaf 100644 --- a/src/core/memory.cpp +++ b/src/core/memory.cpp @@ -10,11 +10,14 @@ #include "memory.h" -#include #include +#include +#include "../cxxutil.hpp" #include "../z_zone.h" +using namespace srb2; + namespace { @@ -58,6 +61,81 @@ void LinearMemory::reset() noexcept } // namespace +PoolAllocator::~PoolAllocator() +{ + release(); +} + +constexpr static size_t nearest_multiple(size_t v, size_t divisor) +{ + return (v + (divisor - 1)) & ~(divisor - 1); +} + +PoolAllocator::ChunkFooter* PoolAllocator::allocate_chunk() +{ + uint8_t* chunk = (uint8_t*)Z_Malloc(nearest_multiple(blocks_ * block_size_, alignof(ChunkFooter)) + sizeof(ChunkFooter), tag_, nullptr); + ChunkFooter* footer = (ChunkFooter*)(chunk + (blocks_ * block_size_)); + footer->next = nullptr; + footer->start = (void*)chunk; + for (size_t i = 0; i < blocks_; i++) + { + FreeBlock* cur = (FreeBlock*)(chunk + (i * block_size_)); + FreeBlock* next = (FreeBlock*)(chunk + ((i + 1) * block_size_)); + cur->next = next; + } + ((FreeBlock*)(chunk + ((blocks_ - 1) * block_size_)))->next = nullptr; + return footer; +} + +void* PoolAllocator::allocate() +{ + if (first_chunk_ == nullptr) + { + SRB2_ASSERT(head_ == nullptr); + + // No chunks allocated yet + first_chunk_ = allocate_chunk(); + head_ = (FreeBlock*)first_chunk_->start; + } + + if (head_->next == nullptr) + { + // Current chunk will be full; allocate another at the end of the list + ChunkFooter* last_chunk = first_chunk_; + while (last_chunk->next != nullptr) + { + last_chunk = last_chunk->next; + } + ChunkFooter* new_chunk = allocate_chunk(); + last_chunk->next = new_chunk; + head_->next = (FreeBlock*)new_chunk->start; + } + + FreeBlock* ret = head_; + head_ = head_->next; + return ret; +} + +void PoolAllocator::deallocate(void* p) +{ + FreeBlock* block = reinterpret_cast(p); + block->next = head_; + head_ = block; +} + +void PoolAllocator::release() +{ + ChunkFooter* next = nullptr; + for (ChunkFooter* i = first_chunk_; i != nullptr; i = next) + { + next = i->next; + Z_Free(i->start); + } + + first_chunk_ = nullptr; + head_ = nullptr; +} + static LinearMemory g_frame_memory {4 * 1024 * 1024}; void* Z_Frame_Alloc(size_t size) diff --git a/src/core/memory.h b/src/core/memory.h index ba49e3d51..d568d34e9 100644 --- a/src/core/memory.h +++ b/src/core/memory.h @@ -14,6 +14,58 @@ #include #ifdef __cplusplus + +#include + +namespace srb2 +{ + +/// Pool allocator; manages bulk allocations of same-size block. If the pool is full, +/// allocations will fail by returning nullptr. +class PoolAllocator final +{ + struct FreeBlock + { + FreeBlock* next; + }; + struct ChunkFooter + { + ChunkFooter* next; + void* start; + }; + ChunkFooter* first_chunk_; + FreeBlock* head_; + size_t block_size_; + size_t blocks_; + int32_t tag_; + + ChunkFooter* allocate_chunk(); + +public: + constexpr PoolAllocator(size_t block_size, size_t blocks, int32_t tag) + : first_chunk_(nullptr) + , head_(nullptr) + , block_size_(block_size) + , blocks_(blocks) + , tag_(tag) + {} + PoolAllocator(const PoolAllocator&) = delete; + PoolAllocator(PoolAllocator&&) noexcept = default; + ~PoolAllocator(); + + PoolAllocator& operator=(const PoolAllocator&) = delete; + PoolAllocator& operator=(PoolAllocator&&) noexcept = default; + + void* allocate(); + void deallocate(void* p); + constexpr size_t block_size() const noexcept { return block_size_; }; + + void release(); +}; + +} // namespace srb2 + + extern "C" { #endif // __cpluspplus diff --git a/src/d_think.h b/src/d_think.h index 1ef2577dc..fc37c64b5 100644 --- a/src/d_think.h +++ b/src/d_think.h @@ -30,6 +30,15 @@ // typedef void (*actionf_p1)(void *); +typedef enum +{ + /// The allocation is standard e.g. Z_Malloc + TAT_MALLOC, + + /// The allocation is in the pool allocator (e.g. Z_LevelPoolCalloc) + TAT_LEVELPOOL +} thinker_alloc_type_e; + // Historically, "think_t" is yet another function pointer to a routine // to handle an actor. typedef actionf_p1 think_t; @@ -44,7 +53,9 @@ typedef struct thinker_s // killough 11/98: count of how many other objects reference // this one using pointers. Used for garbage collection. INT32 references; - boolean cachable; + INT32 alloctype; + size_t size; + #ifdef PARANOIA INT32 debug_mobjtype; tic_t debug_time; diff --git a/src/lua_script.h b/src/lua_script.h index c0eaa192a..ea6c6ec49 100644 --- a/src/lua_script.h +++ b/src/lua_script.h @@ -13,6 +13,10 @@ #ifndef LUA_SCRIPT_H #define LUA_SCRIPT_H +#ifdef __cplusplus +extern "C" { +#endif + #include "m_fixed.h" #include "doomtype.h" #include "d_player.h" @@ -115,4 +119,8 @@ void COM_Lua_f(void); }\ } +#ifdef __cplusplus +} // extern "C" +#endif + #endif/*LUA_SCRIPT_H*/ diff --git a/src/p_ceilng.c b/src/p_ceilng.c index b80656360..8170343c9 100644 --- a/src/p_ceilng.c +++ b/src/p_ceilng.c @@ -11,6 +11,7 @@ /// \file p_ceilng.c /// \brief Ceiling aninmation (lowering, crushing, raising) +#include "d_think.h" #include "doomdef.h" #include "p_local.h" #include "r_fps.h" @@ -396,6 +397,7 @@ void T_CrushCeiling(ceiling_t *ceiling) INT32 EV_DoCeiling(line_t *line, ceiling_e type) { INT32 rtn = 0, firstone = 1; + INT32 secnum = -1; sector_t *sec; ceiling_t *ceiling; @@ -409,7 +411,9 @@ INT32 EV_DoCeiling(line_t *line, ceiling_e type) // new door thinker rtn = 1; - ceiling = Z_Calloc(sizeof (*ceiling), PU_LEVSPEC, NULL); + ceiling = Z_LevelPoolCalloc(sizeof(*ceiling)); + ceiling->thinker.alloctype = TAT_LEVELPOOL; + ceiling->thinker.size = sizeof(*ceiling); P_AddThinker(&ceiling->thinker); sec->ceilingdata = ceiling; ceiling->thinker.function = (actionf_p1)T_MoveCeiling; @@ -619,6 +623,7 @@ INT32 EV_DoCeiling(line_t *line, ceiling_e type) INT32 EV_DoCrush(line_t *line, ceiling_e type) { INT32 rtn = 0; + INT32 secnum = -1; sector_t *sec; ceiling_t *ceiling; @@ -632,7 +637,9 @@ INT32 EV_DoCrush(line_t *line, ceiling_e type) // new door thinker rtn = 1; - ceiling = Z_Calloc(sizeof (*ceiling), PU_LEVSPEC, NULL); + ceiling = Z_LevelPoolCalloc(sizeof(*ceiling)); + ceiling->thinker.alloctype = TAT_LEVELPOOL; + ceiling->thinker.size = sizeof(*ceiling); P_AddThinker(&ceiling->thinker); sec->ceilingdata = ceiling; ceiling->thinker.function = (actionf_p1)T_CrushCeiling; diff --git a/src/p_floor.c b/src/p_floor.c index 86447c5ba..f4cc3e992 100644 --- a/src/p_floor.c +++ b/src/p_floor.c @@ -2538,7 +2538,9 @@ INT32 EV_DoFloor(line_t *line, floor_e floortype) // new floor thinker rtn = 1; - dofloor = Z_Calloc(sizeof (*dofloor), PU_LEVSPEC, NULL); + dofloor = Z_LevelPoolCalloc(sizeof(*dofloor)); + dofloor->thinker.alloctype = TAT_LEVELPOOL; + dofloor->thinker.size = sizeof(*dofloor); P_AddThinker(&dofloor->thinker); // make sure another floor thinker won't get started over this one @@ -2762,7 +2764,9 @@ INT32 EV_DoElevator(line_t *line, elevator_e elevtype, boolean customspeed) // create and initialize new elevator thinker rtn = 1; - elevator = Z_Calloc(sizeof (*elevator), PU_LEVSPEC, NULL); + elevator = Z_LevelPoolCalloc(sizeof(*elevator)); + elevator->thinker.alloctype = TAT_LEVELPOOL; + elevator->thinker.size = sizeof(*elevator); P_AddThinker(&elevator->thinker); sec->floordata = elevator; sec->ceilingdata = elevator; @@ -2951,7 +2955,9 @@ INT32 EV_BounceSector(sector_t *sec, fixed_t momz, line_t *sourceline) if (sec->ceilingdata) // One at a time, ma'am. return 0; - bouncer = Z_Calloc(sizeof (*bouncer), PU_LEVSPEC, NULL); + bouncer = Z_LevelPoolCalloc(sizeof(*bouncer)); + bouncer->thinker.alloctype = TAT_LEVELPOOL; + bouncer->thinker.size = sizeof(*bouncer); P_AddThinker(&bouncer->thinker); sec->ceilingdata = bouncer; bouncer->thinker.function = (actionf_p1)T_BounceCheese; @@ -2989,7 +2995,9 @@ INT32 EV_DoContinuousFall(sector_t *sec, sector_t *backsector, fixed_t spd, bool backsector = sec; // create and initialize new thinker - faller = Z_Calloc(sizeof (*faller), PU_LEVSPEC, NULL); + faller = Z_LevelPoolCalloc(sizeof(*faller)); + faller->thinker.alloctype = TAT_LEVELPOOL; + faller->thinker.size = sizeof(*faller); P_AddThinker(&faller->thinker); faller->thinker.function = (actionf_p1)T_ContinuousFalling; @@ -3042,7 +3050,9 @@ INT32 EV_StartCrumble(sector_t *sec, ffloor_t *rover, boolean floating, return 0; // create and initialize new elevator thinker - elevator = Z_Calloc(sizeof (*elevator), PU_LEVSPEC, NULL); + elevator = Z_LevelPoolCalloc(sizeof(*elevator)); + elevator->thinker.alloctype = TAT_LEVELPOOL; + elevator->thinker.size = sizeof(*elevator); P_AddThinker(&elevator->thinker); elevator->thinker.function = (actionf_p1)T_StartCrumble; @@ -3118,8 +3128,9 @@ INT32 EV_MarioBlock(sector_t *sec, sector_t *roversector, fixed_t topheight, mob { const boolean itsamonitor = (thing->flags & MF_MONITOR) == MF_MONITOR; // create and initialize new elevator thinker - - block = Z_Calloc(sizeof (*block), PU_LEVSPEC, NULL); + block = Z_LevelPoolCalloc(sizeof(*block)); + block->thinker.alloctype = TAT_LEVELPOOL; + block->thinker.size = sizeof(*block); P_AddThinker(&block->thinker); sec->floordata = block; sec->ceilingdata = block; diff --git a/src/p_lights.c b/src/p_lights.c index ba7be57d7..8f033d646 100644 --- a/src/p_lights.c +++ b/src/p_lights.c @@ -74,7 +74,9 @@ fireflicker_t *P_SpawnAdjustableFireFlicker(sector_t *minsector, sector_t *maxse fireflicker_t *flick; P_RemoveLighting(maxsector); // out with the old, in with the new - flick = Z_Calloc(sizeof (*flick), PU_LEVSPEC, NULL); + flick = Z_LevelPoolCalloc(sizeof(*flick)); + flick->thinker.alloctype = TAT_LEVELPOOL; + flick->thinker.size = sizeof(*flick); P_AddThinker(&flick->thinker); @@ -153,7 +155,9 @@ void P_SpawnLightningFlash(sector_t *sector) sector->lightingdata = NULL; - flash = Z_Calloc(sizeof (*flash), PU_LEVSPEC, NULL); + flash = Z_LevelPoolCalloc(sizeof(*flash)); + flash->thinker.alloctype = TAT_LEVELPOOL; + flash->thinker.size = sizeof(*flash); P_AddThinker(&flash->thinker); @@ -212,7 +216,9 @@ strobe_t *P_SpawnAdjustableStrobeFlash(sector_t *minsector, sector_t *maxsector, strobe_t *flash; P_RemoveLighting(maxsector); // out with the old, in with the new - flash = Z_Calloc(sizeof (*flash), PU_LEVSPEC, NULL); + flash = Z_LevelPoolCalloc(sizeof(*flash)); + flash->thinker.alloctype = TAT_LEVELPOOL; + flash->thinker.size = sizeof(*flash); P_AddThinker(&flash->thinker); @@ -287,7 +293,9 @@ glow_t *P_SpawnAdjustableGlowingLight(sector_t *minsector, sector_t *maxsector, glow_t *g; P_RemoveLighting(maxsector); // out with the old, in with the new - g = Z_Calloc(sizeof (*g), PU_LEVSPEC, NULL); + g = Z_LevelPoolCalloc(sizeof(*g)); + g->thinker.alloctype = TAT_LEVELPOOL; + g->thinker.size = sizeof(*g); P_AddThinker(&g->thinker); @@ -346,7 +354,9 @@ void P_FadeLight(INT16 tag, INT32 destvalue, INT32 speed) sector = §ors[i]; P_RemoveLighting(sector); // remove the old lighting effect first - ll = Z_Calloc(sizeof (*ll), PU_LEVSPEC, NULL); + ll = Z_LevelPoolCalloc(sizeof(*ll)); + ll->thinker.alloctype = TAT_LEVELPOOL; + ll->thinker.size = sizeof(*ll); ll->thinker.function = (actionf_p1)T_LightFade; sector->lightingdata = ll; // set it to the lightlevel_t diff --git a/src/p_local.h b/src/p_local.h index b8ea3f056..ab9c82705 100644 --- a/src/p_local.h +++ b/src/p_local.h @@ -67,7 +67,6 @@ // both the head and tail of the thinker list extern thinker_t thinkercap; extern thinker_t precipcap; -extern mobj_t *mobjcache; void P_InitThinkers(void); void P_AddThinker(thinker_t *thinker); @@ -262,6 +261,7 @@ extern consvar_t cv_gravity; void P_RespawnSpecials(void); +mobj_t *P_AllocateMobj(void); mobj_t *P_SpawnMobj(fixed_t x, fixed_t y, fixed_t z, mobjtype_t type); mobj_t *P_SpawnShadowMobj(mobj_t * caster); diff --git a/src/p_mobj.c b/src/p_mobj.c index 74a09d4a0..00951fbe3 100644 --- a/src/p_mobj.c +++ b/src/p_mobj.c @@ -11,6 +11,7 @@ /// \file p_mobj.c /// \brief Moving object handling. Spawn functions +#include "d_think.h" #include "doomdef.h" #include "g_game.h" #include "g_input.h" @@ -47,7 +48,6 @@ actioncache_t actioncachehead; static mobj_t *overlaycap = NULL; static mobj_t *shadowcap = NULL; mobj_t *waypointcap = NULL; -mobj_t *mobjcache = NULL; void P_InitCachedActions(void) { @@ -9499,21 +9499,11 @@ void P_SceneryThinker(mobj_t *mobj) // GAME SPAWN FUNCTIONS // -FUNCINLINE static ATTRINLINE mobj_t *P_AllocMobj(void) +mobj_t *P_AllocateMobj(void) { - mobj_t *mobj; - - if (mobjcache != NULL) - { - mobj = mobjcache; - mobjcache = mobjcache->hnext; - memset(mobj, 0, sizeof(*mobj)); - } - else - { - mobj = Z_Calloc(sizeof (*mobj), PU_LEVEL, NULL); - } - + mobj_t* mobj = (mobj_t*)Z_LevelPoolCalloc(sizeof(mobj_t)); + mobj->thinker.alloctype = TAT_LEVELPOOL; + mobj->thinker.size = sizeof(mobj_t); return mobj; } @@ -9526,7 +9516,7 @@ mobj_t *P_SpawnMobj(fixed_t x, fixed_t y, fixed_t z, mobjtype_t type) state_t *st; mobj_t *mobj; - mobj = P_AllocMobj(); + mobj = P_AllocateMobj(); // this is officially a mobj, declared as soon as possible. mobj->thinker.function = (actionf_p1)P_MobjThinker; @@ -9977,7 +9967,7 @@ mobj_t *P_SpawnShadowMobj(mobj_t * caster) state_t *st; mobj_t *mobj; - mobj = P_AllocMobj(); + mobj = P_AllocateMobj(); // this is officially a mobj, declared as soon as possible. mobj->thinker.function = (actionf_p1)P_MobjThinker; @@ -10091,7 +10081,9 @@ static precipmobj_t *P_SpawnPrecipMobj(fixed_t x, fixed_t y, fixed_t z, mobjtype { const mobjinfo_t *info = &mobjinfo[type]; state_t *st; - precipmobj_t *mobj = Z_Calloc(sizeof (*mobj), PU_LEVEL, NULL); + precipmobj_t *mobj = Z_LevelPoolCalloc(sizeof(precipmobj_t)); + mobj->thinker.alloctype = TAT_LEVELPOOL; + mobj->thinker.size = sizeof(precipmobj_t); fixed_t starting_floorz; mobj->type = type; @@ -10247,14 +10239,6 @@ void P_RemoveMobj(mobj_t *mobj) // free block if (mobj->flags & MF_NOTHINK && !mobj->thinker.next) { // Uh-oh, the mobj doesn't think, P_RemoveThinker would never go through! - if (!mobj->thinker.references) - { - // no references, dump it directly in the mobj cache - mobj->hnext = mobjcache; - mobjcache = mobj; - return; - } - // Add thinker just to delay removing it until refrences are gone. mobj->flags &= ~MF_NOTHINK; P_AddThinker((thinker_t *)mobj); diff --git a/src/p_polyobj.c b/src/p_polyobj.c index d1fb7f1de..d427930df 100644 --- a/src/p_polyobj.c +++ b/src/p_polyobj.c @@ -155,7 +155,6 @@ FUNCINLINE static ATTRINLINE void PolyObj_AddThinker(thinker_t *th) thinkercap.next = th; th->references = 0; - th->cachable = false; // not initialising this made the mobjcache die thx! #ifdef PARANOIA th->debug_mobjtype = MT_NULL; #endif @@ -2358,7 +2357,9 @@ INT32 EV_DoPolyObjRotate(polyrotdata_t *prdata) return 0; // create a new thinker - th = Z_Malloc(sizeof(polyrotate_t), PU_LEVSPEC, NULL); + th = Z_LevelPoolMalloc(sizeof(polyrotate_t)); + th->thinker.alloctype = TAT_LEVELPOOL; + th->thinker.size = sizeof(polyrotate_t); th->thinker.function = (actionf_p1)T_PolyObjRotate; PolyObj_AddThinker(&th->thinker); po->thinker = &th->thinker; @@ -2426,7 +2427,9 @@ INT32 EV_DoPolyObjMove(polymovedata_t *pmdata) return 0; // create a new thinker - th = Z_Malloc(sizeof(polymove_t), PU_LEVSPEC, NULL); + th = Z_LevelPoolMalloc(sizeof(polymove_t)); + th->thinker.alloctype = TAT_LEVELPOOL; + th->thinker.size = sizeof(polymove_t); th->thinker.function = (actionf_p1)T_PolyObjMove; PolyObj_AddThinker(&th->thinker); po->thinker = &th->thinker; @@ -2492,7 +2495,9 @@ INT32 EV_DoPolyObjWaypoint(polywaypointdata_t *pwdata) return 0; // create a new thinker - th = Z_Malloc(sizeof(polywaypoint_t), PU_LEVSPEC, NULL); + th = Z_LevelPoolMalloc(sizeof(polywaypoint_t)); + th->thinker.alloctype = TAT_LEVELPOOL; + th->thinker.size = sizeof(polywaypoint_t); th->thinker.function = (actionf_p1)T_PolyObjWaypoint; PolyObj_AddThinker(&th->thinker); po->thinker = &th->thinker; @@ -2628,7 +2633,9 @@ static void Polyobj_doSlideDoor(polyobj_t *po, polydoordata_t *doordata) INT32 start; // allocate and add a new slide door thinker - th = Z_Malloc(sizeof(polyslidedoor_t), PU_LEVSPEC, NULL); + th = Z_LevelPoolMalloc(sizeof(polyslidedoor_t)); + th->thinker.alloctype = TAT_LEVELPOOL; + th->thinker.size = sizeof(polyslidedoor_t); th->thinker.function = (actionf_p1)T_PolyDoorSlide; PolyObj_AddThinker(&th->thinker); @@ -2679,7 +2686,9 @@ static void Polyobj_doSwingDoor(polyobj_t *po, polydoordata_t *doordata) INT32 start; // allocate and add a new swing door thinker - th = Z_Malloc(sizeof(polyswingdoor_t), PU_LEVSPEC, NULL); + th = Z_LevelPoolMalloc(sizeof(polyswingdoor_t)); + th->thinker.alloctype = TAT_LEVELPOOL; + th->thinker.size = sizeof(polyswingdoor_t); th->thinker.function = (actionf_p1)T_PolyDoorSwing; PolyObj_AddThinker(&th->thinker); @@ -2764,7 +2773,9 @@ INT32 EV_DoPolyObjDisplace(polydisplacedata_t *prdata) return 0; // create a new thinker - th = Z_Malloc(sizeof(polydisplace_t), PU_LEVSPEC, NULL); + th = Z_LevelPoolMalloc(sizeof(polydisplace_t)); + th->thinker.alloctype = TAT_LEVELPOOL; + th->thinker.size = sizeof(polydisplace_t); th->thinker.function = (actionf_p1)T_PolyObjDisplace; PolyObj_AddThinker(&th->thinker); po->thinker = &th->thinker; @@ -2868,7 +2879,9 @@ INT32 EV_DoPolyObjFlag(line_t *pfdata) } // create a new thinker - th = Z_Malloc(sizeof(polymove_t), PU_LEVSPEC, NULL); + th = Z_LevelPoolMalloc(sizeof(polymove_t)); + th->thinker.alloctype = TAT_LEVELPOOL; + th->thinker.size = sizeof(polymove_t); th->thinker.function = (actionf_p1)T_PolyObjFlag; PolyObj_AddThinker(&th->thinker); po->thinker = &th->thinker; @@ -2901,5 +2914,4 @@ INT32 EV_DoPolyObjFlag(line_t *pfdata) return 1; } - // EOF diff --git a/src/p_saveg.c b/src/p_saveg.c index 920bda875..49872a95d 100644 --- a/src/p_saveg.c +++ b/src/p_saveg.c @@ -11,6 +11,7 @@ /// \file p_saveg.c /// \brief Archiving: SaveGame I/O +#include "d_think.h" #include "doomdef.h" #include "byteptr.h" #include "d_main.h" @@ -1926,24 +1927,6 @@ static inline player_t *LoadPlayer(UINT32 player) // Loads a mobj_t from a save game // -FUNCINLINE static ATTRINLINE mobj_t *AllocMobj(void) -{ - mobj_t *mobj; - - if (mobjcache != NULL) - { - mobj = mobjcache; - mobjcache = mobjcache->hnext; - memset(mobj, 0, sizeof(*mobj)); - } - else - { - mobj = Z_Calloc(sizeof (*mobj), PU_LEVEL, NULL); - } - - return mobj; -} - static mobjtype_t g_doomednum_to_mobjtype[UINT16_MAX]; static void CalculateDoomednumToMobjtype(void) @@ -1990,12 +1973,12 @@ static void LoadMobjThinker(savebuffer_t *save, actionf_p1 thinker) return; } - mobj = AllocMobj(); + mobj = P_AllocateMobj(); mobj->spawnpoint = &mapthings[spawnpointnum]; mapthings[spawnpointnum].mobj = mobj; } else - mobj = AllocMobj(); + mobj = P_AllocateMobj(); // declare this as a valid mobj as soon as possible. mobj->thinker.function = thinker; @@ -2234,9 +2217,13 @@ static void LoadMobjThinker(savebuffer_t *save, actionf_p1 thinker) // static void LoadSpecialLevelThinker(savebuffer_t *save, actionf_p1 thinker, UINT8 floorOrCeiling) { - levelspecthink_t *ht = Z_Malloc(sizeof (*ht), PU_LEVSPEC, NULL); + levelspecthink_t *ht = (levelspecthink_t*)Z_LevelPoolMalloc(sizeof (*ht)); + ht->thinker.alloctype = TAT_LEVELPOOL; + ht->thinker.size = sizeof (*ht); + size_t i; ht->thinker.function = thinker; + for (i = 0; i < 16; i++) { ht->vars[i] = READFIXED(save->p); //var[16] @@ -2269,7 +2256,9 @@ static void LoadSpecialLevelThinker(savebuffer_t *save, actionf_p1 thinker, UINT // static void LoadCeilingThinker(savebuffer_t *save, actionf_p1 thinker) { - ceiling_t *ht = Z_Malloc(sizeof (*ht), PU_LEVSPEC, NULL); + ceiling_t *ht = (ceiling_t*)Z_LevelPoolMalloc(sizeof (*ht)); + ht->thinker.alloctype = TAT_LEVELPOOL; + ht->thinker.size = sizeof (*ht); ht->thinker.function = thinker; ht->type = READUINT8(save->p); ht->sector = LoadSector(READUINT32(save->p)); @@ -2301,7 +2290,9 @@ static void LoadCeilingThinker(savebuffer_t *save, actionf_p1 thinker) // static void LoadFloormoveThinker(savebuffer_t *save, actionf_p1 thinker) { - floormove_t *ht = Z_Malloc(sizeof (*ht), PU_LEVSPEC, NULL); + floormove_t *ht = (floormove_t*)Z_LevelPoolMalloc(sizeof (*ht)); + ht->thinker.alloctype = TAT_LEVELPOOL; + ht->thinker.size = sizeof (*ht); ht->thinker.function = thinker; ht->type = READUINT8(save->p); ht->crush = READUINT8(save->p); @@ -2328,7 +2319,9 @@ static void LoadFloormoveThinker(savebuffer_t *save, actionf_p1 thinker) // static void LoadLightflashThinker(savebuffer_t *save, actionf_p1 thinker) { - lightflash_t *ht = Z_Malloc(sizeof (*ht), PU_LEVSPEC, NULL); + lightflash_t *ht = (lightflash_t*)Z_LevelPoolMalloc(sizeof (*ht)); + ht->thinker.alloctype = TAT_LEVELPOOL; + ht->thinker.size = sizeof (*ht); ht->thinker.function = thinker; ht->sector = LoadSector(READUINT32(save->p)); ht->maxlight = READINT32(save->p); @@ -2345,7 +2338,9 @@ static void LoadLightflashThinker(savebuffer_t *save, actionf_p1 thinker) // static void LoadStrobeThinker(savebuffer_t *save, actionf_p1 thinker) { - strobe_t *ht = Z_Malloc(sizeof (*ht), PU_LEVSPEC, NULL); + strobe_t *ht = (strobe_t*)Z_LevelPoolMalloc(sizeof (*ht)); + ht->thinker.alloctype = TAT_LEVELPOOL; + ht->thinker.size = sizeof (*ht); ht->thinker.function = thinker; ht->sector = LoadSector(READUINT32(save->p)); ht->count = READINT32(save->p); @@ -2365,7 +2360,9 @@ static void LoadStrobeThinker(savebuffer_t *save, actionf_p1 thinker) // static void LoadGlowThinker(savebuffer_t *save, actionf_p1 thinker) { - glow_t *ht = Z_Malloc(sizeof (*ht), PU_LEVSPEC, NULL); + glow_t *ht = (glow_t*)Z_LevelPoolMalloc(sizeof (*ht)); + ht->thinker.alloctype = TAT_LEVELPOOL; + ht->thinker.size = sizeof (*ht); ht->thinker.function = thinker; ht->sector = LoadSector(READUINT32(save->p)); ht->minlight = READINT32(save->p); @@ -2384,7 +2381,9 @@ static void LoadGlowThinker(savebuffer_t *save, actionf_p1 thinker) // static void LoadFireflickerThinker(savebuffer_t *save, actionf_p1 thinker) { - fireflicker_t *ht = Z_Malloc(sizeof (*ht), PU_LEVSPEC, NULL); + fireflicker_t *ht = (fireflicker_t*)Z_LevelPoolMalloc(sizeof (*ht)); + ht->thinker.alloctype = TAT_LEVELPOOL; + ht->thinker.size = sizeof (*ht); ht->thinker.function = thinker; ht->sector = LoadSector(READUINT32(save->p)); ht->count = READINT32(save->p); @@ -2403,7 +2402,9 @@ static void LoadFireflickerThinker(savebuffer_t *save, actionf_p1 thinker) // static void LoadElevatorThinker(savebuffer_t *save, actionf_p1 thinker, UINT8 floorOrCeiling) { - elevator_t *ht = Z_Malloc(sizeof (*ht), PU_LEVSPEC, NULL); + elevator_t *ht = (elevator_t*)Z_LevelPoolMalloc(sizeof (*ht)); + ht->thinker.alloctype = TAT_LEVELPOOL; + ht->thinker.size = sizeof (*ht); ht->thinker.function = thinker; ht->type = READUINT8(save->p); ht->sector = LoadSector(READUINT32(save->p)); @@ -2447,7 +2448,9 @@ static void LoadElevatorThinker(savebuffer_t *save, actionf_p1 thinker, UINT8 fl // static void LoadScrollThinker(savebuffer_t *save, actionf_p1 thinker) { - scroll_t *ht = Z_Malloc(sizeof (*ht), PU_LEVSPEC, NULL); + scroll_t *ht = (scroll_t*)Z_LevelPoolMalloc(sizeof (*ht)); + ht->thinker.alloctype = TAT_LEVELPOOL; + ht->thinker.size = sizeof (*ht); ht->thinker.function = thinker; ht->dx = READFIXED(save->p); ht->dy = READFIXED(save->p); @@ -2485,7 +2488,9 @@ static void LoadScrollThinker(savebuffer_t *save, actionf_p1 thinker) // static inline void LoadFrictionThinker(savebuffer_t *save, actionf_p1 thinker) { - friction_t *ht = Z_Malloc(sizeof (*ht), PU_LEVSPEC, NULL); + friction_t *ht = (friction_t*)Z_LevelPoolMalloc(sizeof (*ht)); + ht->thinker.alloctype = TAT_LEVELPOOL; + ht->thinker.size = sizeof (*ht); ht->thinker.function = thinker; ht->friction = READINT32(save->p); ht->movefactor = READINT32(save->p); @@ -2502,7 +2507,9 @@ static inline void LoadFrictionThinker(savebuffer_t *save, actionf_p1 thinker) // static void LoadPusherThinker(savebuffer_t *save, actionf_p1 thinker) { - pusher_t *ht = Z_Malloc(sizeof (*ht), PU_LEVSPEC, NULL); + pusher_t *ht = (pusher_t*)Z_LevelPoolMalloc(sizeof (*ht)); + ht->thinker.alloctype = TAT_LEVELPOOL; + ht->thinker.size = sizeof (*ht); ht->thinker.function = thinker; ht->type = READUINT8(save->p); ht->x_mag = READINT32(save->p); @@ -2528,7 +2535,9 @@ static void LoadPusherThinker(savebuffer_t *save, actionf_p1 thinker) // static inline void LoadLaserThinker(savebuffer_t *save, actionf_p1 thinker) { - laserthink_t *ht = Z_Malloc(sizeof (*ht), PU_LEVSPEC, NULL); + laserthink_t *ht = (laserthink_t*)Z_LevelPoolMalloc(sizeof (*ht)); + ht->thinker.alloctype = TAT_LEVELPOOL; + ht->thinker.size = sizeof (*ht); ffloor_t *rover = NULL; ht->thinker.function = thinker; ht->sector = LoadSector(READUINT32(save->p)); @@ -2548,7 +2557,9 @@ static inline void LoadLaserThinker(savebuffer_t *save, actionf_p1 thinker) // FUNCINLINE static ATTRINLINE void LoadLightlevelThinker(savebuffer_t *save, actionf_p1 thinker) { - lightlevel_t *ht = Z_Malloc(sizeof (*ht), PU_LEVSPEC, NULL); + lightlevel_t *ht = (lightlevel_t*)Z_LevelPoolMalloc(sizeof (*ht)); + ht->thinker.alloctype = TAT_LEVELPOOL; + ht->thinker.size = sizeof (*ht); ht->thinker.function = thinker; ht->sector = LoadSector(READUINT32(save->p)); ht->destlevel = READINT32(save->p); @@ -2565,7 +2576,9 @@ FUNCINLINE static ATTRINLINE void LoadLightlevelThinker(savebuffer_t *save, acti // static inline void LoadExecutorThinker(savebuffer_t *save, actionf_p1 thinker) { - executor_t *ht = Z_Malloc(sizeof (*ht), PU_LEVSPEC, NULL); + executor_t *ht = (executor_t*)Z_LevelPoolMalloc(sizeof (*ht)); + ht->thinker.alloctype = TAT_LEVELPOOL; + ht->thinker.size = sizeof (*ht); ht->thinker.function = thinker; ht->line = LoadLine(READUINT32(save->p)); ht->caller = LoadMobj(READUINT32(save->p)); @@ -2581,7 +2594,9 @@ static inline void LoadExecutorThinker(savebuffer_t *save, actionf_p1 thinker) // static inline void LoadDisappearThinker(savebuffer_t *save, actionf_p1 thinker) { - disappear_t *ht = Z_Malloc(sizeof (*ht), PU_LEVSPEC, NULL); + disappear_t *ht = (disappear_t*)Z_LevelPoolMalloc(sizeof (*ht)); + ht->thinker.alloctype = TAT_LEVELPOOL; + ht->thinker.size = sizeof (*ht); ht->thinker.function = thinker; ht->appeartime = READUINT32(save->p); ht->disappeartime = READUINT32(save->p); @@ -2600,7 +2615,9 @@ static inline void LoadDisappearThinker(savebuffer_t *save, actionf_p1 thinker) // static inline void LoadPolyrotatetThinker(savebuffer_t *save, actionf_p1 thinker) { - polyrotate_t *ht = Z_Malloc(sizeof (*ht), PU_LEVSPEC, NULL); + polyrotate_t *ht = (polyrotate_t*)Z_LevelPoolMalloc(sizeof (*ht)); + ht->thinker.alloctype = TAT_LEVELPOOL; + ht->thinker.size = sizeof (*ht); ht->thinker.function = thinker; ht->polyObjNum = READINT32(save->p); ht->speed = READINT32(save->p); @@ -2627,7 +2644,9 @@ static inline void LoadPolyrotatetThinker(savebuffer_t *save, actionf_p1 thinker // static void LoadPolymoveThinker(savebuffer_t *save, actionf_p1 thinker) { - polymove_t *ht = Z_Malloc(sizeof (*ht), PU_LEVSPEC, NULL); + polymove_t *ht = (polymove_t*)Z_LevelPoolMalloc(sizeof (*ht)); + ht->thinker.alloctype = TAT_LEVELPOOL; + ht->thinker.size = sizeof (*ht); ht->thinker.function = thinker; ht->polyObjNum = READINT32(save->p); ht->speed = READINT32(save->p); @@ -2656,7 +2675,9 @@ static void LoadPolymoveThinker(savebuffer_t *save, actionf_p1 thinker) // static inline void LoadPolywaypointThinker(savebuffer_t *save, actionf_p1 thinker) { - polywaypoint_t *ht = Z_Malloc(sizeof (*ht), PU_LEVSPEC, NULL); + polywaypoint_t *ht = (polywaypoint_t*)Z_LevelPoolMalloc(sizeof (*ht)); + ht->thinker.alloctype = TAT_LEVELPOOL; + ht->thinker.size = sizeof (*ht); ht->thinker.function = thinker; ht->polyObjNum = READINT32(save->p); ht->speed = READINT32(save->p); @@ -2710,7 +2731,9 @@ static inline void LoadPolywaypointThinker(savebuffer_t *save, actionf_p1 thinke // static inline void LoadPolyslidedoorThinker(savebuffer_t *save, actionf_p1 thinker) { - polyslidedoor_t *ht = Z_Malloc(sizeof (*ht), PU_LEVSPEC, NULL); + polyslidedoor_t *ht = (polyslidedoor_t*)Z_LevelPoolMalloc(sizeof (*ht)); + ht->thinker.alloctype = TAT_LEVELPOOL; + ht->thinker.size = sizeof (*ht); ht->thinker.function = thinker; ht->polyObjNum = READINT32(save->p); ht->delay = READINT32(save->p); @@ -2746,7 +2769,9 @@ static inline void LoadPolyslidedoorThinker(savebuffer_t *save, actionf_p1 think // static inline void LoadPolyswingdoorThinker(savebuffer_t *save, actionf_p1 thinker) { - polyswingdoor_t *ht = Z_Malloc(sizeof (*ht), PU_LEVSPEC, NULL); + polyswingdoor_t *ht = (polyswingdoor_t*)Z_LevelPoolMalloc(sizeof (*ht)); + ht->thinker.alloctype = TAT_LEVELPOOL; + ht->thinker.size = sizeof (*ht); ht->thinker.function = thinker; ht->polyObjNum = READINT32(save->p); ht->delay = READINT32(save->p); @@ -2777,7 +2802,9 @@ static inline void LoadPolyswingdoorThinker(savebuffer_t *save, actionf_p1 think // static inline void LoadPolydisplaceThinker(savebuffer_t *save, actionf_p1 thinker) { - polydisplace_t *ht = Z_Malloc(sizeof (*ht), PU_LEVSPEC, NULL); + polydisplace_t *ht = (polydisplace_t*)Z_LevelPoolMalloc(sizeof (*ht)); + ht->thinker.alloctype = TAT_LEVELPOOL; + ht->thinker.size = sizeof (*ht); ht->thinker.function = thinker; ht->polyObjNum = READINT32(save->p); ht->controlSector = LoadSector(READUINT32(save->p)); @@ -2829,7 +2856,14 @@ static void P_NetUnArchiveThinkers(savebuffer_t *save) { (next->prev = currentthinker->prev)->next = next; R_DestroyLevelInterpolators(currentthinker); - Z_Free(currentthinker); + if (currentthinker->alloctype == TAT_LEVELPOOL) + { + Z_LevelPoolFree(currentthinker, currentthinker->size); + } + else + { + Z_Free(currentthinker); + } } } diff --git a/src/p_setup.c b/src/p_setup.c index e2998705e..c8849b95d 100644 --- a/src/p_setup.c +++ b/src/p_setup.c @@ -2908,8 +2908,6 @@ boolean P_SetupLevel(boolean fromnetsave, boolean reloadinggamestate) R_ClearLevelSplats(); #endif - mobjcache = NULL; - R_InitializeLevelInterpolators(); P_InitThinkers(); diff --git a/src/p_slopes.c b/src/p_slopes.c index 499e8dca3..850813331 100644 --- a/src/p_slopes.c +++ b/src/p_slopes.c @@ -10,6 +10,7 @@ /// \file p_slopes.c /// \brief ZDoom + Eternity Engine Slopes, ported and enhanced by Kalaron +#include "d_think.h" #include "doomdef.h" #include "r_defs.h" #include "r_state.h" diff --git a/src/p_spec.c b/src/p_spec.c index 23ac8a0d4..a2940af66 100644 --- a/src/p_spec.c +++ b/src/p_spec.c @@ -15,6 +15,7 @@ /// utility functions, etc. /// Line Tag handling. Line and Sector triggers. +#include "d_think.h" #include "doomdef.h" #include "g_game.h" #include "p_local.h" @@ -1300,7 +1301,9 @@ static void P_AddExecutorDelay(line_t *line, mobj_t *mobj, sector_t *sector) if (!line->backsector) I_Error("P_AddExecutorDelay: Line has no backsector!\n"); - e = Z_Calloc(sizeof (*e), PU_LEVSPEC, NULL); + e = Z_LevelPoolCalloc(sizeof (*e)); + e->thinker.alloctype = TAT_LEVELPOOL; + e->thinker.size = sizeof (*e); e->thinker.function = (actionf_p1)T_ExecutorDelay; e->line = line; @@ -4907,7 +4910,10 @@ static void P_AddSpikeThinker(sector_t *sec, INT32 referrer) levelspecthink_t *spikes; // create and initialize new thinker - spikes = Z_Calloc(sizeof (*spikes), PU_LEVSPEC, NULL); + spikes = Z_LevelPoolCalloc(sizeof (*spikes)); + spikes->thinker.alloctype = TAT_LEVELPOOL; + spikes->thinker.size = sizeof (*spikes); + P_AddThinker(&spikes->thinker); spikes->thinker.function = (actionf_p1)T_SpikeSector; @@ -4929,7 +4935,9 @@ static void P_AddFloatThinker(sector_t *sec, INT32 tag, line_t *sourceline) levelspecthink_t *floater; // create and initialize new thinker - floater = Z_Calloc(sizeof (*floater), PU_LEVSPEC, NULL); + floater = Z_LevelPoolCalloc(sizeof (*floater)); + floater->thinker.alloctype = TAT_LEVELPOOL; + floater->thinker.size = sizeof (*floater); P_AddThinker(&floater->thinker); floater->thinker.function = (actionf_p1)T_FloatSector; @@ -4958,7 +4966,9 @@ static void P_AddBlockThinker(sector_t *sec, line_t *sourceline) levelspecthink_t *block; // create and initialize new elevator thinker - block = Z_Calloc(sizeof (*block), PU_LEVSPEC, NULL); + block = Z_LevelPoolCalloc(sizeof (*block)); + block->thinker.alloctype = TAT_LEVELPOOL; + block->thinker.size = sizeof (*block); P_AddThinker(&block->thinker); block->thinker.function = (actionf_p1)T_MarioBlockChecker; @@ -4987,7 +4997,9 @@ static void P_AddRaiseThinker(sector_t *sec, line_t *sourceline) { levelspecthink_t *raise; - raise = Z_Calloc(sizeof (*raise), PU_LEVSPEC, NULL); + raise = Z_LevelPoolCalloc(sizeof (*raise)); + raise->thinker.alloctype = TAT_LEVELPOOL; + raise->thinker.size = sizeof (*raise); P_AddThinker(&raise->thinker); raise->thinker.function = (actionf_p1)T_RaiseSector; @@ -5030,7 +5042,9 @@ static void P_AddOldAirbob(sector_t *sec, line_t *sourceline, boolean noadjust) { levelspecthink_t *airbob; - airbob = Z_Calloc(sizeof (*airbob), PU_LEVSPEC, NULL); + airbob = Z_LevelPoolCalloc(sizeof (*airbob)); + airbob->thinker.alloctype = TAT_LEVELPOOL; + airbob->thinker.size = sizeof (*airbob); P_AddThinker(&airbob->thinker); airbob->thinker.function = (actionf_p1)T_RaiseSector; @@ -5095,7 +5109,9 @@ static inline void P_AddThwompThinker(sector_t *sec, sector_t *actionsector, lin return; // create and initialize new elevator thinker - thwomp = Z_Calloc(sizeof (*thwomp), PU_LEVSPEC, NULL); + thwomp = Z_LevelPoolCalloc(sizeof (*thwomp)); + thwomp->thinker.alloctype = TAT_LEVELPOOL; + thwomp->thinker.size = sizeof (*thwomp); P_AddThinker(&thwomp->thinker); thwomp->thinker.function = (actionf_p1)T_ThwompSector; @@ -5136,7 +5152,9 @@ static inline void P_AddNoEnemiesThinker(sector_t *sec, line_t *sourceline) levelspecthink_t *nobaddies; // create and initialize new thinker - nobaddies = Z_Calloc(sizeof (*nobaddies), PU_LEVSPEC, NULL); + nobaddies = Z_LevelPoolCalloc(sizeof (*nobaddies)); + nobaddies->thinker.alloctype = TAT_LEVELPOOL; + nobaddies->thinker.size = sizeof (*nobaddies); P_AddThinker(&nobaddies->thinker); nobaddies->thinker.function = (actionf_p1)T_NoEnemiesSector; @@ -5158,7 +5176,9 @@ static inline void P_AddEachTimeThinker(sector_t *sec, line_t *sourceline) levelspecthink_t *eachtime; // create and initialize new thinker - eachtime = Z_Calloc(sizeof (*eachtime), PU_LEVSPEC, NULL); + eachtime = Z_LevelPoolCalloc(sizeof (*eachtime)); + eachtime->thinker.alloctype = TAT_LEVELPOOL; + eachtime->thinker.size = sizeof (*eachtime); P_AddThinker(&eachtime->thinker); eachtime->thinker.function = (actionf_p1)T_EachTimeThinker; @@ -5180,7 +5200,9 @@ static inline void P_AddCameraScanner(sector_t *sourcesec, sector_t *actionsecto elevator_t *elevator; // Why not? LOL // create and initialize new elevator thinker - elevator = Z_Calloc(sizeof (*elevator), PU_LEVSPEC, NULL); + elevator = Z_LevelPoolCalloc(sizeof (*elevator)); + elevator->thinker.alloctype = TAT_LEVELPOOL; + elevator->thinker.size = sizeof (*elevator); P_AddThinker(&elevator->thinker); elevator->thinker.function = (actionf_p1)T_CameraScanner; @@ -5262,13 +5284,14 @@ void T_LaserFlash(laserthink_t *flash) */ static inline void EV_AddLaserThinker(sector_t *sec, sector_t *sec2, line_t *line, thinkerlist_t *secthinkers) { - laserthink_t *flash; ffloor_t *ffloor = P_AddFakeFloor(sec, sec2, line, laserflags, secthinkers); if (!ffloor) return; - flash = Z_Calloc(sizeof (*flash), PU_LEVSPEC, NULL); + laserthink_t *flash = Z_LevelPoolCalloc(sizeof (*flash)); + flash->thinker.alloctype = TAT_LEVELPOOL; + flash->thinker.size = sizeof (*flash); P_AddThinker(&flash->thinker); @@ -6636,8 +6659,11 @@ void T_Scroll(scroll_t *s) */ static void Add_Scroller(INT32 type, fixed_t dx, fixed_t dy, INT32 control, INT32 affectee, INT32 accel, INT32 exclusive) { - scroll_t *s = Z_Calloc(sizeof *s, PU_LEVSPEC, NULL); + scroll_t *s = Z_LevelPoolCalloc(sizeof (*s)); + s->thinker.alloctype = TAT_LEVELPOOL; + s->thinker.size = sizeof (*s); s->thinker.function = (actionf_p1)T_Scroll; + s->type = type; s->dx = dx; s->dy = dy; @@ -6805,7 +6831,9 @@ static void P_SpawnScrollers(void) */ static void Add_MasterDisappearer(tic_t appeartime, tic_t disappeartime, tic_t offset, INT32 line, INT32 sourceline) { - disappear_t *d = Z_Malloc(sizeof *d, PU_LEVSPEC, NULL); + disappear_t *d = Z_LevelPoolCalloc(sizeof (*d)); + d->thinker.alloctype = TAT_LEVELPOOL; + d->thinker.size = sizeof (*d); d->thinker.function = (actionf_p1)T_Disappear; d->appeartime = appeartime; @@ -6890,7 +6918,9 @@ void T_Disappear(disappear_t *d) */ static void Add_Friction(INT32 friction, INT32 movefactor, INT32 affectee, INT32 referrer) { - friction_t *f = Z_Calloc(sizeof *f, PU_LEVSPEC, NULL); + friction_t *f = Z_LevelPoolCalloc(sizeof (*f)); + f->thinker.alloctype = TAT_LEVELPOOL; + f->thinker.size = sizeof (*f); f->thinker.function = (actionf_p1)T_Friction; f->friction = friction; @@ -7045,7 +7075,9 @@ static void P_SpawnFriction(void) */ static void Add_Pusher(pushertype_e type, fixed_t x_mag, fixed_t y_mag, mobj_t *source, INT32 affectee, INT32 referrer, INT32 exclusive, INT32 slider) { - pusher_t *p = Z_Calloc(sizeof *p, PU_LEVSPEC, NULL); + pusher_t *p = Z_LevelPoolCalloc(sizeof (*p)); + p->thinker.alloctype = TAT_LEVELPOOL; + p->thinker.size = sizeof (*p); p->thinker.function = (actionf_p1)T_Pusher; p->source = source; diff --git a/src/p_tick.c b/src/p_tick.c index 9a2b6e60c..e6755fec7 100644 --- a/src/p_tick.c +++ b/src/p_tick.c @@ -15,6 +15,7 @@ #include "dehacked.h" // MOBJTYPE_LIST #endif +#include "d_think.h" #include "doomstat.h" #include "dehacked.h" #include "g_game.h" @@ -207,8 +208,6 @@ void P_AddThinker(thinker_t *thinker) thinkercap.prev = thinker; thinker->references = 0; // killough 11/98: init reference counter to 0 - - thinker->cachable = (thinker->function == (actionf_p1)P_MobjThinker); #ifdef PARANOIA thinker->debug_mobjtype = MT_NULL; #endif @@ -226,8 +225,6 @@ void P_AddPrecipThinker(thinker_t *thinker) precipcap.prev = thinker; thinker->references = 0; // killough 11/98: init reference counter to 0 - - thinker->cachable = false; #ifdef PARANOIA thinker->debug_mobjtype = MT_NULL; #endif @@ -345,11 +342,9 @@ void P_UnlinkThinker(thinker_t *thinker) (next->prev = thinker->prev)->next = next; - if (thinker->cachable == true) + if (thinker->alloctype == TAT_LEVELPOOL) { - // put cachable thinkers in the mobj cache, so we can avoid allocations - ((mobj_t *)thinker)->hnext = mobjcache; - mobjcache = (mobj_t *)thinker; + Z_LevelPoolFree(thinker, thinker->size); } else { diff --git a/src/z_zone.c b/src/z_zone.cpp similarity index 90% rename from src/z_zone.c rename to src/z_zone.cpp index ea6dc3fec..4b7b76507 100644 --- a/src/z_zone.c +++ b/src/z_zone.cpp @@ -28,6 +28,7 @@ #include #include +#include "core/memory.h" #include "doomdef.h" #include "doomstat.h" #include "r_patch.h" @@ -77,6 +78,16 @@ typedef struct memblock_s // both the head and tail of the zone memory block list static memblock_t head; +static constexpr size_t kLevelLargePoolBlockSize = sizeof(mobj_t); +static constexpr size_t kLevelMedPoolBlockSize = sizeof(precipmobj_t); +static constexpr size_t kLevelSmallPoolBlockSize = 128; +static constexpr size_t kLevelTinyPoolBlockSize = 64; + +static srb2::PoolAllocator g_level_large_pool { kLevelLargePoolBlockSize, 1024, PU_LEVEL }; +static srb2::PoolAllocator g_level_med_pool { kLevelMedPoolBlockSize, 32768, PU_LEVEL }; +static srb2::PoolAllocator g_level_small_pool { kLevelSmallPoolBlockSize, 4096, PU_LEVEL }; +static srb2::PoolAllocator g_level_tiny_pool { kLevelTinyPoolBlockSize, 8192, PU_LEVEL }; + // // Function prototypes // @@ -230,7 +241,7 @@ void *Z_Malloc(size_t size, INT32 tag, void *user) CONS_Debug(DBG_MEMORY, "Z_Malloc %s:%d\n", file, line); #endif - block = xm(sizeof (memblock_t) + ALIGNPAD + size); + block = (memblock_t*)xm(sizeof (memblock_t) + ALIGNPAD + size); ptr = MEMORY(block); I_Assert((intptr_t)ptr % alignof (max_align_t) == 0); @@ -260,7 +271,7 @@ void *Z_Malloc(size_t size, INT32 tag, void *user) if (user != NULL) { - block->user = user; + block->user = (void**)user; *(void **)user = ptr; } else if (tag >= PU_PURGELEVEL) @@ -407,6 +418,16 @@ void Z_FreeTags(INT32 lowtag, INT32 hightag) #else Z_CheckHeap(420); #endif + + // First, release all pools, since they can make allocations in zones. + if (PU_LEVEL >= lowtag && PU_LEVEL <= hightag) + { + g_level_large_pool.release(); + g_level_med_pool.release(); + g_level_small_pool.release(); + g_level_tiny_pool.release(); + } + for (block = head.next; block != &head; block = next) { next = block->next; // get link before freeing @@ -658,7 +679,7 @@ void Z_SetUser(void *ptr, void **newuser) I_Error("Internal memory management error: " "tried to make block purgable but it has no owner"); - block->user = (void*)newuser; + block->user = (void**)newuser; *newuser = ptr; } @@ -756,7 +777,7 @@ static void Command_Memdump_f(void) for (block = head.next; block != &head; block = block->next) if (block->tag >= mintag && block->tag <= maxtag) { - char *filename = strrchr(block->ownerfile, PATHSEP[0]); + const char *filename = strrchr(block->ownerfile, PATHSEP[0]); CONS_Printf("[%3d] %s (%s) bytes @ %s:%d\n", block->tag, sizeu1(block->size), sizeu2(block->realsize), filename ? filename + 1 : block->ownerfile, block->ownerline); } } @@ -769,5 +790,61 @@ static void Command_Memdump_f(void) */ char *Z_StrDup(const char *s) { - return strcpy(ZZ_Alloc(strlen(s) + 1), s); + return strcpy((char*)ZZ_Alloc(strlen(s) + 1), s); +} + +void* Z_LevelPoolMalloc(size_t size) +{ + void* p = nullptr; + if (size <= kLevelTinyPoolBlockSize) + { + p = g_level_tiny_pool.allocate(); + } + else if (size <= kLevelSmallPoolBlockSize) + { + p = g_level_small_pool.allocate(); + } + else if (size <= kLevelMedPoolBlockSize) + { + p = g_level_med_pool.allocate(); + } + else if (size <= kLevelLargePoolBlockSize) + { + p = g_level_large_pool.allocate(); + } + + if (p == nullptr) + { + p = Z_Malloc(size, PU_LEVEL, nullptr); + } + + return p; +} + +void* Z_LevelPoolCalloc(size_t size) +{ + void* p = Z_LevelPoolMalloc(size); + memset(p, 0, size); + return p; +} + +void Z_LevelPoolFree(void* p, size_t size) +{ + if (size <= kLevelTinyPoolBlockSize) + { + return g_level_tiny_pool.deallocate(p); + } + if (size <= kLevelSmallPoolBlockSize) + { + return g_level_small_pool.deallocate(p); + } + if (size <= kLevelMedPoolBlockSize) + { + return g_level_med_pool.deallocate(p); + } + if (size <= kLevelLargePoolBlockSize) + { + return g_level_large_pool.deallocate(p); + } + return Z_Free(p); } diff --git a/src/z_zone.h b/src/z_zone.h index adbabb37e..709fa1d96 100644 --- a/src/z_zone.h +++ b/src/z_zone.h @@ -164,6 +164,13 @@ size_t Z_TagsUsage(INT32 lowtag, INT32 hightag); // char *Z_StrDup(const char *in); +// +// Specialty allocation functions +// +void *Z_LevelPoolMalloc(size_t size); +void *Z_LevelPoolCalloc(size_t size); +void Z_LevelPoolFree(void *p, size_t size); + #ifdef __cplusplus } // extern "C" #endif