mirror of
https://github.com/ClassiCube/ClassiCube.git
synced 2025-01-22 09:01:57 -05:00
32x: Launcher displays better now
This commit is contained in:
parent
12d127248c
commit
f77b5897fa
11 changed files with 628 additions and 40 deletions
5
.gitignore
vendored
5
.gitignore
vendored
|
@ -154,6 +154,11 @@ openvcl/
|
|||
*.pidb
|
||||
*.svclog
|
||||
*.scc
|
||||
*.map
|
||||
|
||||
# Binary files
|
||||
*.bin
|
||||
*.elf
|
||||
|
||||
# Visual C++ cache files
|
||||
ipch/
|
||||
|
|
|
@ -136,11 +136,8 @@ void Hw32xScreenClear()
|
|||
|
||||
Hw32xSetFGColor(255,31,31,31);
|
||||
Hw32xSetBGColor(0,0,0,0);
|
||||
X = Y = 0;
|
||||
}
|
||||
|
||||
extern unsigned char msx[];
|
||||
|
||||
void Hw32xDelay(int ticks)
|
||||
{
|
||||
unsigned long ct = MARS_SYS_COMM12 + ticks;
|
||||
|
|
|
@ -304,6 +304,9 @@ void LBackend_ThemeChanged(void) { LBackend_Redraw(); }
|
|||
|
||||
void LBackend_Tick(void) {
|
||||
int i;
|
||||
/* Window backend requires always redrawing entire frame - slow */
|
||||
/* Most window backends do not require this though */
|
||||
if (DisplayInfo.FullRedraw && pendingRedraw) pendingRedraw |= REDRAW_ALL;
|
||||
DoRedraw();
|
||||
|
||||
if (pendingFullDraws) {
|
||||
|
|
|
@ -18,6 +18,7 @@
|
|||
#include "_PlatformConsole.h"
|
||||
#include "../misc/32x/32x.h"
|
||||
#include "../misc/32x/hw_32x.h"
|
||||
#include "../third_party/tinyalloc/tinyalloc.c"
|
||||
|
||||
const cc_result ReturnCode_FileShareViolation = 1000000000; // not used
|
||||
const cc_result ReturnCode_FileNotFound = 99999;
|
||||
|
@ -37,25 +38,24 @@ cc_bool Platform_ReadonlyFilesystem = true;
|
|||
void* Mem_TryAlloc(cc_uint32 numElems, cc_uint32 elemsSize) {
|
||||
cc_uint32 size = CalcMemSize(numElems, elemsSize);
|
||||
Platform_Log1(" MALLOC: %i", &size);
|
||||
void* ptr = size ? malloc(size) : NULL;
|
||||
void* ptr = size ? ta_alloc(size) : NULL;
|
||||
Platform_Log1("MALLOCED: %x", &ptr);
|
||||
return ptr;
|
||||
}
|
||||
|
||||
void* Mem_TryAllocCleared(cc_uint32 numElems, cc_uint32 elemsSize) {
|
||||
Platform_Log1(" CALLOC: %i", &numElems);
|
||||
void* ptr = calloc(numElems, elemsSize);
|
||||
void* ptr = ta_calloc(numElems, elemsSize);
|
||||
Platform_Log1("CALLOCED: %x", &ptr);
|
||||
return ptr;
|
||||
}
|
||||
|
||||
void* Mem_TryRealloc(void* mem, cc_uint32 numElems, cc_uint32 elemsSize) {
|
||||
cc_uint32 size = CalcMemSize(numElems, elemsSize);
|
||||
return size ? realloc(mem, size) : NULL;
|
||||
return NULL; // TODO
|
||||
}
|
||||
|
||||
void Mem_Free(void* mem) {
|
||||
if (mem) free(mem);
|
||||
if (mem) ta_free(mem);
|
||||
}
|
||||
|
||||
|
||||
|
@ -94,8 +94,10 @@ cc_uint64 Stopwatch_ElapsedMicroseconds(cc_uint64 beg, cc_uint64 end) {
|
|||
return 1;
|
||||
}
|
||||
|
||||
extern void _bss_end;
|
||||
static void Stopwatch_Init(void) {
|
||||
// TODO
|
||||
ta_init(&_bss_end, (void*)(0x0603FFFF - 16384), 256, 16, 4);
|
||||
}
|
||||
|
||||
|
||||
|
@ -161,6 +163,7 @@ cc_result File_Length(cc_file file, cc_uint32* len) {
|
|||
*#########################################################################################################################*/
|
||||
void Thread_Sleep(cc_uint32 milliseconds) {
|
||||
// TODO sleep a bit
|
||||
Hw32xDelay(1);
|
||||
}
|
||||
|
||||
void Thread_Run(void** handle, Thread_StartFunc func, int stackSize, const char* name) {
|
||||
|
|
|
@ -143,7 +143,7 @@ static void SPConnection_BeginConnect(void) {
|
|||
#endif
|
||||
World_SetDimensions(horSize, verSize, horSize);
|
||||
|
||||
#if defined CC_BUILD_N64 || defined CC_BUILD_NDS || defined CC_BUILD_PS1 || defined CC_BUILD_SATURN
|
||||
#if defined CC_BUILD_N64 || defined CC_BUILD_NDS || defined CC_BUILD_PS1 || defined CC_BUILD_SATURN || defined CC_BUILD_32X
|
||||
Gen_Active = &FlatgrassGen;
|
||||
#else
|
||||
Gen_Active = &NotchyGen;
|
||||
|
|
|
@ -40,6 +40,9 @@ CC_VAR extern struct _DisplayData {
|
|||
cc_bool ShowingSoftKeyboard;
|
||||
/* Whether the cursor is currently visible */
|
||||
cc_bool CursorVisible;
|
||||
/* Whether the framebuffer must always be entirely redrawn */
|
||||
/* NOTE: Currently only the Sega 32X requires this */
|
||||
cc_bool FullRedraw;
|
||||
/* Amount to offset content near the edges of the window by */
|
||||
/* Mainly intended for when the game is rendered on TV displays, where */
|
||||
/* pixels on the edges of the screen may be hidden due to overscan */
|
||||
|
|
|
@ -43,6 +43,7 @@ void Window_Init(void) {
|
|||
|
||||
DisplayInfo.ContentOffsetX = 10;
|
||||
DisplayInfo.ContentOffsetY = 10;
|
||||
DisplayInfo.FullRedraw = true;
|
||||
|
||||
Hw32xInit(MARS_VDP_MODE_32K, 0);
|
||||
}
|
||||
|
@ -128,39 +129,9 @@ void Window_AllocFramebuffer(struct Bitmap* bmp, int width, int height) {
|
|||
bmp->height = height;
|
||||
}
|
||||
|
||||
// TODO ????
|
||||
static void DrawFramebuffer(Rect2D r, struct Bitmap* bmp, int mode) {
|
||||
MARS_VDP_FBCTL = mode;
|
||||
while ((MARS_VDP_FBCTL & MARS_VDP_FS) != mode);
|
||||
|
||||
Platform_LogConst("DRAW");
|
||||
}
|
||||
|
||||
void Window_DrawFramebuffer(Rect2D r, struct Bitmap* bmp) {
|
||||
DrawFramebuffer(r, bmp, 0);
|
||||
DrawFramebuffer(r, bmp, 1);
|
||||
Hw32xScreenFlip(true);
|
||||
}
|
||||
/*
|
||||
MARS_VDP_FBCTL = 1;
|
||||
while ((MARS_VDP_FBCTL & MARS_VDP_FS) != 1);
|
||||
|
||||
Hw32xSetFGColor(255,31,31,31);
|
||||
Hw32xSetBGColor(0,0,0,0);
|
||||
|
||||
volatile uint16_t* vram = &MARS_FRAMEBUFFER + 0x100;
|
||||
Platform_LogConst("DRAW");
|
||||
|
||||
// TODO: Partial redraws seem to produce some corrupt pixels ???
|
||||
for (int y = r.y; y < r.y + r.height; y++)
|
||||
{
|
||||
BitmapCol* row = Bitmap_GetRow(bmp, y);
|
||||
for (int x = r.x; x < r.x + r.width; x++)
|
||||
{
|
||||
// TODO optimise
|
||||
vram[x + (y * 320)] = 0x8FFF;
|
||||
}
|
||||
}
|
||||
}*/
|
||||
|
||||
void Window_FreeFramebuffer(struct Bitmap* bmp) {
|
||||
}
|
||||
|
|
201
third_party/tinyalloc/LICENSE
vendored
Normal file
201
third_party/tinyalloc/LICENSE
vendored
Normal file
|
@ -0,0 +1,201 @@
|
|||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction,
|
||||
and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by
|
||||
the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all
|
||||
other entities that control, are controlled by, or are under common
|
||||
control with that entity. For the purposes of this definition,
|
||||
"control" means (i) the power, direct or indirect, to cause the
|
||||
direction or management of such entity, whether by contract or
|
||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity
|
||||
exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications,
|
||||
including but not limited to software source code, documentation
|
||||
source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical
|
||||
transformation or translation of a Source form, including but
|
||||
not limited to compiled object code, generated documentation,
|
||||
and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or
|
||||
Object form, made available under the License, as indicated by a
|
||||
copyright notice that is included in or attached to the work
|
||||
(an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object
|
||||
form, that is based on (or derived from) the Work and for which the
|
||||
editorial revisions, annotations, elaborations, or other modifications
|
||||
represent, as a whole, an original work of authorship. For the purposes
|
||||
of this License, Derivative Works shall not include works that remain
|
||||
separable from, or merely link (or bind by name) to the interfaces of,
|
||||
the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including
|
||||
the original version of the Work and any modifications or additions
|
||||
to that Work or Derivative Works thereof, that is intentionally
|
||||
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||
or by an individual or Legal Entity authorized to submit on behalf of
|
||||
the copyright owner. For the purposes of this definition, "submitted"
|
||||
means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems,
|
||||
and issue tracking systems that are managed by, or on behalf of, the
|
||||
Licensor for the purpose of discussing and improving the Work, but
|
||||
excluding communication that is conspicuously marked or otherwise
|
||||
designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||
on behalf of whom a Contribution has been received by Licensor and
|
||||
subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the
|
||||
Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
(except as stated in this section) patent license to make, have made,
|
||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||
where such license applies only to those patent claims licensable
|
||||
by such Contributor that are necessarily infringed by their
|
||||
Contribution(s) alone or by combination of their Contribution(s)
|
||||
with the Work to which such Contribution(s) was submitted. If You
|
||||
institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||
or a Contribution incorporated within the Work constitutes direct
|
||||
or contributory patent infringement, then any patent licenses
|
||||
granted to You under this License for that Work shall terminate
|
||||
as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the
|
||||
Work or Derivative Works thereof in any medium, with or without
|
||||
modifications, and in Source or Object form, provided that You
|
||||
meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or
|
||||
Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices
|
||||
stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works
|
||||
that You distribute, all copyright, patent, trademark, and
|
||||
attribution notices from the Source form of the Work,
|
||||
excluding those notices that do not pertain to any part of
|
||||
the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its
|
||||
distribution, then any Derivative Works that You distribute must
|
||||
include a readable copy of the attribution notices contained
|
||||
within such NOTICE file, excluding those notices that do not
|
||||
pertain to any part of the Derivative Works, in at least one
|
||||
of the following places: within a NOTICE text file distributed
|
||||
as part of the Derivative Works; within the Source form or
|
||||
documentation, if provided along with the Derivative Works; or,
|
||||
within a display generated by the Derivative Works, if and
|
||||
wherever such third-party notices normally appear. The contents
|
||||
of the NOTICE file are for informational purposes only and
|
||||
do not modify the License. You may add Your own attribution
|
||||
notices within Derivative Works that You distribute, alongside
|
||||
or as an addendum to the NOTICE text from the Work, provided
|
||||
that such additional attribution notices cannot be construed
|
||||
as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and
|
||||
may provide additional or different license terms and conditions
|
||||
for use, reproduction, or distribution of Your modifications, or
|
||||
for any such Derivative Works as a whole, provided Your use,
|
||||
reproduction, and distribution of the Work otherwise complies with
|
||||
the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||
any Contribution intentionally submitted for inclusion in the Work
|
||||
by You to the Licensor shall be under the terms and conditions of
|
||||
this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify
|
||||
the terms of any separate license agreement you may have executed
|
||||
with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade
|
||||
names, trademarks, service marks, or product names of the Licensor,
|
||||
except as required for reasonable and customary use in describing the
|
||||
origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||
agreed to in writing, Licensor provides the Work (and each
|
||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
implied, including, without limitation, any warranties or conditions
|
||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||
appropriateness of using or redistributing the Work and assume any
|
||||
risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory,
|
||||
whether in tort (including negligence), contract, or otherwise,
|
||||
unless required by applicable law (such as deliberate and grossly
|
||||
negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special,
|
||||
incidental, or consequential damages of any character arising as a
|
||||
result of this License or out of the use or inability to use the
|
||||
Work (including but not limited to damages for loss of goodwill,
|
||||
work stoppage, computer failure or malfunction, or any and all
|
||||
other commercial damages or losses), even if such Contributor
|
||||
has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing
|
||||
the Work or Derivative Works thereof, You may choose to offer,
|
||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||
or other liability obligations and/or rights consistent with this
|
||||
License. However, in accepting such obligations, You may act only
|
||||
on Your own behalf and on Your sole responsibility, not on behalf
|
||||
of any other Contributor, and only if You agree to indemnify,
|
||||
defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason
|
||||
of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
APPENDIX: How to apply the Apache License to your work.
|
||||
|
||||
To apply the Apache License to your work, attach the following
|
||||
boilerplate notice, with the fields enclosed by brackets "{}"
|
||||
replaced with your own identifying information. (Don't include
|
||||
the brackets!) The text should be enclosed in the appropriate
|
||||
comment syntax for the file format. We also recommend that a
|
||||
file or class name and description of purpose be included on the
|
||||
same "printed page" as the copyright notice for easier
|
||||
identification within third-party archives.
|
||||
|
||||
Copyright {yyyy} {name of copyright owner}
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
115
third_party/tinyalloc/README.md
vendored
Normal file
115
third_party/tinyalloc/README.md
vendored
Normal file
|
@ -0,0 +1,115 @@
|
|||
# thi.ng/tinyalloc
|
||||
|
||||
Tiny replacement for `malloc` / `free` in unmanaged, linear memory situations, e.g. [WASM](http://webassembly.org) and [embedded devices](https://github.com/thi-ng/ws-ldn-12).
|
||||
|
||||
## Updates
|
||||
|
||||
For an updated version (written in TypeScript, but still targetting the same linear
|
||||
memory setup) with more features and improved block splitting/coalescing, please visit:
|
||||
[thi.ng/malloc](https://github.com/thi-ng/umbrella/tree/develop/packages/malloc).
|
||||
|
||||
For an in-depth discussion and comparison with other allocators, please see:
|
||||
|
||||
- [Toward Specialization of Memory
|
||||
Management in Unikernels (Hugo Lefeuvre)](https://os.itec.kit.edu/downloads/2020_BA_Lefeuvre_Toward_Specialization_of_Memory_Management_in_Unikernels.pdf)
|
||||
|
||||
## Features
|
||||
|
||||
- written in standalone C11, no dependencies, C runtime or syscalls used
|
||||
- configurable address region (and max. block count) for heap space
|
||||
- configurable pointer alignment in heap space
|
||||
- optional compaction of consecutive free blocks
|
||||
- optional block splitting during alloc (if re-using larger free'd blocks)
|
||||
- tiny, the WASM binary is 1.5KB (1.1KB w/ compaction disabled)
|
||||
|
||||
## Details
|
||||
|
||||
**tinyalloc** maintains 3 linked lists: fresh blocks, used blocks, free blocks. All lists are stored in the same fixed sized array so the memory overhead can be controlled at compile time via the [configuration vars](#configuration) listed below. During initialization all blocks are added to the list of fresh blocks.
|
||||
|
||||
The difference between free & fresh blocks is the former already have an associated heap address and size from previous usage. OTOH fresh blocks are uninitialized and are only used if no existing free blocks satisfy an allocation request.
|
||||
|
||||
The diagram illustrates the state of having 1 freed block (green), 2 used blocks (red) and the beginning of the fresh (unused) block list:
|
||||
|
||||
![memory layout](tinyalloc.png)
|
||||
|
||||
### Allocation
|
||||
|
||||
When a new chunk of memory is requested, all previously freed blocks are checked for potential re-use. If a block is found, is larger than the requested size and the size difference is greater than the configured threshold (`TA_SPLIT_THRESH`), then the block is first split, the chunks added to the used & free lists respectively and the pointer to the first chunk returned to the user. If no blocks in the free list qualify, a new block is allocated at the current heap top address, moved from the "fresh" to the "used" block list and the pointer returned to the caller.
|
||||
|
||||
Note: All returned pointers are aligned to `TA_ALIGN` word boundaries. Same goes for allocated block sizes. Also, allocation will fail when all blocks in the fixed size block array are used, even though there might still be ample space in the heap memory region...
|
||||
|
||||
### Freeing & compaction
|
||||
|
||||
The list of freed blocks is sorted by block start address. When a block is being freed, **tinyalloc** uses insertion sort to add the block at the right list position and then runs a compaction procedure, merging blocks as long as they form consecutive chunks of memory (with no gaps in between them). The resulting obsolete blocks are re-added to the list of available blocks.
|
||||
|
||||
## API
|
||||
|
||||
### ta\_init(void \*base, void \*limit, size_t heap_blocks, size_t split_thresh, size_t alignment)
|
||||
|
||||
Initializes the control datastructure. MUST be called prior to any other **tinyalloc** function.
|
||||
|
||||
| Argument | Description |
|
||||
|----------|-------------|
|
||||
| `base` | Address of **tinyalloc** control structure, typically at the beginning of your heap |
|
||||
| `limit` | Heap space end address |
|
||||
| `heap_blocks` | Max. number of memory chunks (e.g. 256) |
|
||||
| `split_thresh` | Size threshold for splitting chunks (a good default is 16) |
|
||||
| `alignment` | Word size for pointer alignment (e.g. 8) |
|
||||
|
||||
- `alignment` is assumed to be >= native word size
|
||||
- `base` must be an address in RAM (on embedded devices)
|
||||
|
||||
### void* ta\_alloc(size\_t num)
|
||||
|
||||
Like standard `malloc`, returns aligned pointer to address in heap space, or `NULL` if allocation failed.
|
||||
|
||||
### void* ta\_calloc(size\_t num, size\_t t)
|
||||
|
||||
Like standard `calloc`, returns aligned pointer to zeroed memory in heap space, or `NULL` if allocation failed.
|
||||
|
||||
### bool ta\_free(void \*ptr)
|
||||
|
||||
Like `free`, but returns boolean result (true, if freeing succeeded). By default, any consecutive memory blocks are being merged during the freeing operation.
|
||||
|
||||
### bool ta\_check()
|
||||
|
||||
Structural validation. Returns `true` if internal heap structure is ok.
|
||||
|
||||
## Building
|
||||
|
||||
### Configuration
|
||||
|
||||
| Define | Default | Comment |
|
||||
|--------|---------|---------|
|
||||
| `TA_DEBUG` | undefined | Trace debug information |
|
||||
| `TA_DISABLE_COMPACT` | undefined | Disable free block compaction |
|
||||
| `TA_DISABLE_SPLIT` | undefined | Disable free block splitting during re-alloc |
|
||||
|
||||
On a 32bit system, the default configuration causes an overhead of 3088 bytes in RAM, but can be reduced if fewer memory blocks are needed.
|
||||
|
||||
**Notes:**
|
||||
|
||||
If building in debug mode (if `TA_DEBUG` symbol is defined), two externally defined functions are required:
|
||||
|
||||
- `print_s(char *)` - to print a single string
|
||||
- `print_i(size_t)` - to print a single unsigned int
|
||||
|
||||
### Building for WASM
|
||||
|
||||
(Requires [emscripten](http://emscripten.org))
|
||||
|
||||
```sh
|
||||
emcc -Oz -s WASM=1 -s SIDE_MODULE=1 -o tinyalloc.wasm tinyalloc.c
|
||||
```
|
||||
|
||||
#### Disassemble to WAST
|
||||
|
||||
(Requires [WABT](https://github.com/WebAssembly/wabt))
|
||||
|
||||
```sh
|
||||
wasm2wast --generate-names tinyalloc.wasm > tinyalloc.wast
|
||||
```
|
||||
|
||||
## License
|
||||
|
||||
© 2016 - 2017 Karsten Schmidt - Apache Software License 2.0 (see [LICENSE](./LICENSE))
|
270
third_party/tinyalloc/tinyalloc.c
vendored
Normal file
270
third_party/tinyalloc/tinyalloc.c
vendored
Normal file
|
@ -0,0 +1,270 @@
|
|||
#include "tinyalloc.h"
|
||||
#include <stdint.h>
|
||||
|
||||
#ifdef TA_DEBUG
|
||||
extern void print_s(char *);
|
||||
extern void print_i(size_t);
|
||||
#else
|
||||
#define print_s(X)
|
||||
#define print_i(X)
|
||||
#endif
|
||||
|
||||
typedef struct Block Block;
|
||||
|
||||
struct Block {
|
||||
void *addr;
|
||||
Block *next;
|
||||
size_t size;
|
||||
};
|
||||
|
||||
typedef struct {
|
||||
Block *free; // first free block
|
||||
Block *used; // first used block
|
||||
Block *fresh; // first available blank block
|
||||
size_t top; // top free addr
|
||||
} Heap;
|
||||
|
||||
static Heap *heap = NULL;
|
||||
static const void *heap_limit = NULL;
|
||||
static size_t heap_split_thresh;
|
||||
static size_t heap_alignment;
|
||||
static size_t heap_max_blocks;
|
||||
|
||||
/**
|
||||
* If compaction is enabled, inserts block
|
||||
* into free list, sorted by addr.
|
||||
* If disabled, add block has new head of
|
||||
* the free list.
|
||||
*/
|
||||
static void insert_block(Block *block) {
|
||||
#ifndef TA_DISABLE_COMPACT
|
||||
Block *ptr = heap->free;
|
||||
Block *prev = NULL;
|
||||
while (ptr != NULL) {
|
||||
if ((size_t)block->addr <= (size_t)ptr->addr) {
|
||||
print_s("insert");
|
||||
print_i((size_t)ptr);
|
||||
break;
|
||||
}
|
||||
prev = ptr;
|
||||
ptr = ptr->next;
|
||||
}
|
||||
if (prev != NULL) {
|
||||
if (ptr == NULL) {
|
||||
print_s("new tail");
|
||||
}
|
||||
prev->next = block;
|
||||
} else {
|
||||
print_s("new head");
|
||||
heap->free = block;
|
||||
}
|
||||
block->next = ptr;
|
||||
#else
|
||||
block->next = heap->free;
|
||||
heap->free = block;
|
||||
#endif
|
||||
}
|
||||
|
||||
#ifndef TA_DISABLE_COMPACT
|
||||
static void release_blocks(Block *scan, Block *to) {
|
||||
Block *scan_next;
|
||||
while (scan != to) {
|
||||
print_s("release");
|
||||
print_i((size_t)scan);
|
||||
scan_next = scan->next;
|
||||
scan->next = heap->fresh;
|
||||
heap->fresh = scan;
|
||||
scan->addr = 0;
|
||||
scan->size = 0;
|
||||
scan = scan_next;
|
||||
}
|
||||
}
|
||||
|
||||
static void compact() {
|
||||
Block *ptr = heap->free;
|
||||
Block *prev;
|
||||
Block *scan;
|
||||
while (ptr != NULL) {
|
||||
prev = ptr;
|
||||
scan = ptr->next;
|
||||
while (scan != NULL &&
|
||||
(size_t)prev->addr + prev->size == (size_t)scan->addr) {
|
||||
print_s("merge");
|
||||
print_i((size_t)scan);
|
||||
prev = scan;
|
||||
scan = scan->next;
|
||||
}
|
||||
if (prev != ptr) {
|
||||
size_t new_size =
|
||||
(size_t)prev->addr - (size_t)ptr->addr + prev->size;
|
||||
print_s("new size");
|
||||
print_i(new_size);
|
||||
ptr->size = new_size;
|
||||
Block *next = prev->next;
|
||||
// make merged blocks available
|
||||
release_blocks(ptr->next, prev->next);
|
||||
// relink
|
||||
ptr->next = next;
|
||||
}
|
||||
ptr = ptr->next;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
bool ta_init(const void *base, const void *limit, const size_t heap_blocks, const size_t split_thresh, const size_t alignment) {
|
||||
heap = (Heap *)base;
|
||||
heap_limit = limit;
|
||||
heap_split_thresh = split_thresh;
|
||||
heap_alignment = alignment;
|
||||
heap_max_blocks = heap_blocks;
|
||||
|
||||
heap->free = NULL;
|
||||
heap->used = NULL;
|
||||
heap->fresh = (Block *)(heap + 1);
|
||||
heap->top = (size_t)(heap->fresh + heap_blocks);
|
||||
|
||||
Block *block = heap->fresh;
|
||||
size_t i = heap_max_blocks - 1;
|
||||
while (i--) {
|
||||
block->next = block + 1;
|
||||
block++;
|
||||
}
|
||||
block->next = NULL;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ta_free(void *free) {
|
||||
Block *block = heap->used;
|
||||
Block *prev = NULL;
|
||||
while (block != NULL) {
|
||||
if (free == block->addr) {
|
||||
if (prev) {
|
||||
prev->next = block->next;
|
||||
} else {
|
||||
heap->used = block->next;
|
||||
}
|
||||
insert_block(block);
|
||||
#ifndef TA_DISABLE_COMPACT
|
||||
compact();
|
||||
#endif
|
||||
return true;
|
||||
}
|
||||
prev = block;
|
||||
block = block->next;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
static Block *alloc_block(size_t num) {
|
||||
Block *ptr = heap->free;
|
||||
Block *prev = NULL;
|
||||
size_t top = heap->top;
|
||||
num = (num + heap_alignment - 1) & -heap_alignment;
|
||||
while (ptr != NULL) {
|
||||
const int is_top = ((size_t)ptr->addr + ptr->size >= top) && ((size_t)ptr->addr + num <= (size_t)heap_limit);
|
||||
if (is_top || ptr->size >= num) {
|
||||
if (prev != NULL) {
|
||||
prev->next = ptr->next;
|
||||
} else {
|
||||
heap->free = ptr->next;
|
||||
}
|
||||
ptr->next = heap->used;
|
||||
heap->used = ptr;
|
||||
if (is_top) {
|
||||
print_s("resize top block");
|
||||
ptr->size = num;
|
||||
heap->top = (size_t)ptr->addr + num;
|
||||
#ifndef TA_DISABLE_SPLIT
|
||||
} else if (heap->fresh != NULL) {
|
||||
size_t excess = ptr->size - num;
|
||||
if (excess >= heap_split_thresh) {
|
||||
ptr->size = num;
|
||||
Block *split = heap->fresh;
|
||||
heap->fresh = split->next;
|
||||
split->addr = (void *)((size_t)ptr->addr + num);
|
||||
print_s("split");
|
||||
print_i((size_t)split->addr);
|
||||
split->size = excess;
|
||||
insert_block(split);
|
||||
#ifndef TA_DISABLE_COMPACT
|
||||
compact();
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
}
|
||||
return ptr;
|
||||
}
|
||||
prev = ptr;
|
||||
ptr = ptr->next;
|
||||
}
|
||||
// no matching free blocks
|
||||
// see if any other blocks available
|
||||
size_t new_top = top + num;
|
||||
if (heap->fresh != NULL && new_top <= (size_t)heap_limit) {
|
||||
ptr = heap->fresh;
|
||||
heap->fresh = ptr->next;
|
||||
ptr->addr = (void *)top;
|
||||
ptr->next = heap->used;
|
||||
ptr->size = num;
|
||||
heap->used = ptr;
|
||||
heap->top = new_top;
|
||||
return ptr;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void *ta_alloc(size_t num) {
|
||||
Block *block = alloc_block(num);
|
||||
if (block != NULL) {
|
||||
return block->addr;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static void memclear(void *ptr, size_t num) {
|
||||
size_t *ptrw = (size_t *)ptr;
|
||||
size_t numw = (num & -sizeof(size_t)) / sizeof(size_t);
|
||||
while (numw--) {
|
||||
*ptrw++ = 0;
|
||||
}
|
||||
num &= (sizeof(size_t) - 1);
|
||||
uint8_t *ptrb = (uint8_t *)ptrw;
|
||||
while (num--) {
|
||||
*ptrb++ = 0;
|
||||
}
|
||||
}
|
||||
|
||||
void *ta_calloc(size_t num, size_t size) {
|
||||
num *= size;
|
||||
Block *block = alloc_block(num);
|
||||
if (block != NULL) {
|
||||
memclear(block->addr, num);
|
||||
return block->addr;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static size_t count_blocks(Block *ptr) {
|
||||
size_t num = 0;
|
||||
while (ptr != NULL) {
|
||||
num++;
|
||||
ptr = ptr->next;
|
||||
}
|
||||
return num;
|
||||
}
|
||||
|
||||
size_t ta_num_free() {
|
||||
return count_blocks(heap->free);
|
||||
}
|
||||
|
||||
size_t ta_num_used() {
|
||||
return count_blocks(heap->used);
|
||||
}
|
||||
|
||||
size_t ta_num_fresh() {
|
||||
return count_blocks(heap->fresh);
|
||||
}
|
||||
|
||||
bool ta_check() {
|
||||
return heap_max_blocks == ta_num_free() + ta_num_used() + ta_num_fresh();
|
||||
}
|
20
third_party/tinyalloc/tinyalloc.h
vendored
Normal file
20
third_party/tinyalloc/tinyalloc.h
vendored
Normal file
|
@ -0,0 +1,20 @@
|
|||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
|
||||
bool ta_init(const void *base, const void *limit, const size_t heap_blocks, const size_t split_thresh, const size_t alignment);
|
||||
void *ta_alloc(size_t num);
|
||||
void *ta_calloc(size_t num, size_t size);
|
||||
bool ta_free(void *ptr);
|
||||
|
||||
size_t ta_num_free();
|
||||
size_t ta_num_used();
|
||||
size_t ta_num_fresh();
|
||||
bool ta_check();
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
Loading…
Reference in a new issue