This shows you the differences between two versions of the page.
allocating_and_freeing_memory_blocks [2020/06/08 15:46] richardrussell created |
allocating_and_freeing_memory_blocks [2021/01/07 11:16] (current) richardrussell Added FN_heaprealloc() |
||
---|---|---|---|
Line 40: | Line 40: | ||
These routines may not be as efficient or fast as the OS equivalents, and they use up space in BASIC's heap which the OS allocation functions don't, but nevertheless there may be occasions when they are useful. | These routines may not be as efficient or fast as the OS equivalents, and they use up space in BASIC's heap which the OS allocation functions don't, but nevertheless there may be occasions when they are useful. | ||
+ | |||
+ | //January 2021// | ||
+ | |||
+ | When this article was originally published I didn't include an equivalent of the **realloc()** functionality, such as can be achieved in BBCSDL using **SYS "SDL_realloc"**. To rectify that omission here is a function which can re-size a block previously allocated with **FN_heapalloc()**: | ||
+ | |||
+ | <code bb4w> | ||
+ | DEF FN_heaprealloc(p%%, N%) | ||
+ | LOCAL a$ : N% += 23 | ||
+ | SWAP ]^a$, ](p%%-8) | ||
+ | p%% -= PTR(a$) | ||
+ | IF N% < LENa$ a$ = LEFT$(a$,N%) | ||
+ | IF N% > LENa$ a$ += STRING$(N% - LENa$, CHR$0) | ||
+ | p%% += PTR(a$) | ||
+ | SWAP ](p%%-8), ]^a$ | ||
+ | = p%% | ||
+ | </code> | ||
+ | |||
+ | Please note that whilst **FN_heapalloc()** always returns a paragraph-aligned block of memory, **FN_heaprealloc()** doesn't. If it's essential that the memory be aligned you should create a new block of the wanted size, copy the contents of the old block into it, and free the old block. |