LCOV - code coverage report
Current view: top level - AP_Scripting/lua/src - lmem.c (source / functions) Hit Total Coverage
Test: lcov.info Lines: 19 25 76.0 %
Date: 2026-09-09 11:02:08 Functions: 2 3 66.7 %

          Line data    Source code
       1             : /*
       2             : ** $Id: lmem.c,v 1.91.1.1 2017/04/19 17:20:42 roberto Exp $
       3             : ** Interface to Memory Manager
       4             : ** See Copyright Notice in lua.h
       5             : */
       6             : 
       7             : #define lmem_c
       8             : #define LUA_CORE
       9             : 
      10             : #include "lprefix.h"
      11             : 
      12             : 
      13             : #include <stddef.h>
      14             : 
      15             : #include "lua.h"
      16             : 
      17             : #include "ldebug.h"
      18             : #include "ldo.h"
      19             : #include "lgc.h"
      20             : #include "lmem.h"
      21             : #include "lobject.h"
      22             : #include "lstate.h"
      23             : 
      24             : 
      25             : 
      26             : /*
      27             : ** About the realloc function:
      28             : ** void * frealloc (void *ud, void *ptr, size_t osize, size_t nsize);
      29             : ** ('osize' is the old size, 'nsize' is the new size)
      30             : **
      31             : ** * frealloc(ud, NULL, x, s) creates a new block of size 's' (no
      32             : ** matter 'x').
      33             : **
      34             : ** * frealloc(ud, p, x, 0) frees the block 'p'
      35             : ** (in this specific case, frealloc must return NULL);
      36             : ** particularly, frealloc(ud, NULL, 0, 0) does nothing
      37             : ** (which is equivalent to free(NULL) in ISO C)
      38             : **
      39             : ** frealloc returns NULL if it cannot create or reallocate the area
      40             : ** (any reallocation to an equal or smaller size cannot fail!)
      41             : */
      42             : 
      43             : 
      44             : 
      45             : #define MINSIZEARRAY    4
      46             : 
      47             : 
      48       25036 : void *luaM_growaux_ (lua_State *L, void *block, int *size, size_t size_elems,
      49             :                      int limit, const char *what) {
      50             :   void *newblock;
      51             :   int newsize;
      52       25036 :   if (*size >= limit/2) {  /* cannot double it? */
      53           0 :     if (*size >= limit)  /* cannot grow even a little? */
      54           0 :       luaG_runerror(L, "too many %s (limit is %d)", what, limit);
      55           0 :     newsize = limit;  /* still have at least one free place */
      56             :   }
      57             :   else {
      58       25036 :     newsize = (*size)*2;
      59       25036 :     if (newsize < MINSIZEARRAY)
      60        9022 :       newsize = MINSIZEARRAY;  /* minimum size */
      61             :   }
      62       25036 :   newblock = luaM_reallocv(L, block, *size, newsize, size_elems);
      63       25036 :   *size = newsize;  /* update only when everything else is OK */
      64       25036 :   return newblock;
      65             : }
      66             : 
      67             : 
      68           0 : l_noret luaM_toobig (lua_State *L) {
      69           0 :   luaG_runerror(L, "memory allocation error: block too big");
      70             : }
      71             : 
      72             : 
      73             : 
      74             : /*
      75             : ** generic allocation routine.
      76             : */
      77     3641709 : void *luaM_realloc_ (lua_State *L, void *block, size_t osize, size_t nsize) {
      78             :   void *newblock;
      79     3641709 :   global_State *g = G(L);
      80     3641709 :   size_t realosize = (block) ? osize : 0;
      81             :   lua_assert((realosize == 0) == (block == NULL));
      82             : #if defined(HARDMEMTESTS)
      83             :   if (nsize > realosize && g->gcrunning)
      84             :     luaC_fullgc(L, 1);  /* force a GC whenever possible */
      85             : #endif
      86     3641709 :   newblock = (*g->frealloc)(g->ud, block, osize, nsize);
      87     3641709 :   if (newblock == NULL && nsize > 0) {
      88             :     lua_assert(nsize > realosize);  /* cannot fail when shrinking a block */
      89           3 :     if (g->version) {  /* is state fully built? */
      90           3 :       luaC_fullgc(L, 1);  /* try to free some memory... */
      91           3 :       newblock = (*g->frealloc)(g->ud, block, osize, nsize);  /* try again */
      92             :     }
      93           3 :     if (newblock == NULL)
      94           0 :       luaD_throw(L, LUA_ERRMEM);
      95             :   }
      96             :   lua_assert((nsize == 0) == (newblock == NULL));
      97     3641709 :   g->GCdebt = (g->GCdebt + nsize) - realosize;
      98     3641709 :   return newblock;
      99             : }
     100             : 

Generated by: LCOV version 1.14