Created
June 29, 2011 04:47
-
-
Save joeyadams/1053173 to your computer and use it in GitHub Desktop.
Simple, fast memory pool implementation for TI-68k, including demo game.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Copyright (C) 2011 Joseph A. Adams ([email protected]) | |
All rights reserved. | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in | |
all copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
THE SOFTWARE. | |
*/ | |
#include <tigcclib.h> | |
/* | |
* Size per handle of memory allocated from the system. | |
* Also the upper limit on block_size. | |
*/ | |
#define CHUNK_SIZE 4096 | |
typedef struct HPool HPool; | |
typedef struct Extra Extra; | |
typedef struct Block Block; | |
struct HPool | |
{ | |
/* Linked list of unused blocks. */ | |
Block *free_list; | |
/* Size of each block. */ | |
unsigned short block_size; | |
/* Linked list of handles allocated in case we need more memory. */ | |
Extra *extra; | |
}; | |
struct Extra | |
{ | |
Extra *next; | |
}; | |
struct Block | |
{ | |
/* Only when block is free. */ | |
Block *next; | |
}; | |
/* Chain together an array of blocks, forming a free list. */ | |
static Block *parcel(void *memory, unsigned short block_size, unsigned short block_count) | |
{ | |
Block *cur; | |
for (cur = memory; block_count-- > 1; cur = cur->next) | |
cur->next = (Block*) ((char*)cur + block_size); | |
cur->next = NULL; | |
return memory; | |
} | |
/* | |
* Create a new memory pool for allocating blocks of a given size. | |
* On error, return NULL; | |
*/ | |
HPool *hpool_new(unsigned short block_size) | |
{ | |
unsigned short block_count; | |
HPool *hp; | |
if (block_size > CHUNK_SIZE) | |
return NULL; | |
/* Make sure blocks are large enough to act as free list nodes. */ | |
if (block_size < sizeof(Block)) | |
block_size = sizeof(Block); | |
/* | |
* Round up block_size to next even number so | |
* blocks (*cough* free list nodes *cough*) | |
* will be even-aligned. | |
*/ | |
block_size = (block_size + 1) & ~1; | |
block_count = CHUNK_SIZE / block_size; | |
hp = HeapAllocPtr(sizeof(HPool) + block_size * block_count); | |
if (hp == NULL) | |
return NULL; | |
hp->free_list = parcel(hp + 1, block_size, block_count); | |
hp->block_size = block_size; | |
hp->extra = NULL; | |
return hp; | |
} | |
/* Free an HPool (and all the blocks allocated into it). */ | |
void hpool_delete(HPool *hp) | |
{ | |
if (hp != NULL) { | |
Extra *extra = hp->extra; | |
Extra *next; | |
HeapFreePtr(hp); | |
for (; extra != NULL; extra = next) { | |
next = extra->next; | |
HeapFreePtr(extra); | |
} | |
} | |
} | |
/* | |
* Allocate a block from an HPool. It will have hp->block_size bytes available. | |
* If there's not enough memory left, return NULL. | |
*/ | |
void *hpool_alloc(HPool *hp) | |
{ | |
Block *b = hp->free_list; | |
if (__builtin_expect(b == NULL, 0)) { | |
/* Grab another chunk of memory. */ | |
unsigned short block_count = CHUNK_SIZE / hp->block_size; | |
Extra *extra = HeapAllocPtr(sizeof(Extra) + hp->block_size * block_count); | |
if (extra == NULL) | |
return NULL; | |
extra->next = hp->extra; | |
hp->extra = extra; | |
b = parcel(extra + 1, hp->block_size, block_count); | |
} | |
hp->free_list = b->next; | |
return b; | |
} | |
/* Free a single block allocated from an hpool. */ | |
void hpool_free(HPool *hp, void *block) | |
{ | |
if (block != NULL) { | |
Block *b = block; | |
b->next = hp->free_list; | |
hp->free_list = b; | |
} | |
} | |
/* | |
* Balls demo. | |
* | |
* Balls bounce off the left, right, and top of the screen, | |
* and disappear when they reach the bottom of the screen. | |
* The launcher is on the left of the screen. | |
* | |
* Keys: | |
* | |
* Up/Down: Adjust launcher angle. | |
* 2nd: Launch a single ball. | |
* Diamond: Launch a stream of balls. | |
* Esc: Quit the demo. | |
* | |
*/ | |
typedef struct Ball Ball; | |
#define SCALE_FACTOR (1 << (SCALE_SHIFT)) | |
#define SCALE_SHIFT 6 | |
#define BALL_SIZE 4 | |
struct Ball | |
{ | |
Ball *prev, *next; | |
/* Position (of top-left corner of sprite) and velocity, times SCALE_FACTOR. */ | |
int x, y; | |
int dx, dy; | |
}; | |
HPool *hp_Ball; | |
void *back_buffer; | |
volatile int timer; | |
DEFINE_INT_HANDLER(timer_handler) | |
{ | |
timer++; | |
} | |
unsigned char ball_sprite[BALL_SIZE] = { | |
0b01100000, | |
0b11110000, | |
0b11110000, | |
0b01100000, | |
}; | |
void update_ball(Ball *ball) | |
{ | |
const int x_min = 0; | |
const int x_max = ((LCD_WIDTH - BALL_SIZE) << SCALE_SHIFT) + SCALE_FACTOR - 1; | |
const int y_min = 0; | |
const int y_max = ((LCD_HEIGHT - BALL_SIZE) << SCALE_SHIFT) + SCALE_FACTOR - 1; | |
ball->x += ball->dx; | |
ball->y += ball->dy; | |
if (ball->x < x_min) { | |
ball->x = x_min; | |
if (ball->dx < 0) | |
ball->dx = -ball->dx; | |
} else if (ball->x > x_max) { | |
ball->x = x_max; | |
if (ball->dx > 0) | |
ball->dx = -ball->dx; | |
} | |
if (ball->y < y_min) { | |
ball->y = y_min; | |
if (ball->dy < 0) | |
ball->dy = -ball->dy; | |
} else if (ball->y > y_max) { | |
/* Rather than bouncing off the bottom, disappear. */ | |
ball->prev->next = ball->next; | |
ball->next->prev = ball->prev; | |
hpool_free(hp_Ball, ball); | |
} | |
} | |
void draw_ball(Ball *ball) | |
{ | |
int x = ball->x >> SCALE_SHIFT; | |
int y = ball->y >> SCALE_SHIFT; | |
if (x >= 0 && x <= LCD_WIDTH - BALL_SIZE && | |
y >= 0 && y <= LCD_HEIGHT - BALL_SIZE) | |
Sprite8(x, y, BALL_SIZE, ball_sprite, back_buffer, SPRT_OR); | |
} | |
void demo(void) | |
{ | |
Ball balls; /* Circular list */ | |
Ball *ball, *next; | |
float launcher_angle = 0.0; | |
int firing = 0; | |
#define FOREACH_BALL() \ | |
for (ball = balls.next; next = ball->next, ball != &balls; ball = next) | |
balls.prev = &balls; | |
balls.next = &balls; | |
for (;;) { | |
timer = 0; | |
int launcher_dx = cos(launcher_angle) * 100.0; | |
int launcher_dy = sin(launcher_angle) * 100.0; | |
memset(back_buffer, 0, LCD_SIZE); | |
DrawClipLine(&(WIN_RECT){0, LCD_HEIGHT/2, launcher_dx/10, LCD_HEIGHT/2 + launcher_dy/10}, | |
&(SCR_RECT){{0, 0, 239, 127}}, | |
A_NORMAL); | |
FOREACH_BALL() | |
update_ball(ball); | |
FOREACH_BALL() | |
draw_ball(ball); | |
memcpy(LCD_MEM, back_buffer, LCD_SIZE); | |
enum { | |
P_UP = 1, | |
P_DOWN = 2, | |
P_2ND = 4, | |
P_DIAMOND = 8, | |
}; | |
unsigned short keys_pressed = 0; | |
do { | |
if (_keytest(RR_ESC)) | |
return; | |
if (_keytest(RR_UP)) | |
keys_pressed |= P_UP; | |
if (_keytest(RR_DOWN)) | |
keys_pressed |= P_DOWN; | |
if (_keytest(RR_2ND)) | |
keys_pressed |= P_2ND; | |
if (_keytest(RR_DIAMOND)) | |
keys_pressed |= P_DIAMOND; | |
} while (timer < 5); | |
if (keys_pressed & P_UP) { | |
launcher_angle -= 0.05; | |
if (launcher_angle < -PI/2.0) | |
launcher_angle = -PI/2.0; | |
} | |
if (keys_pressed & P_DOWN) { | |
launcher_angle += 0.05; | |
if (launcher_angle > PI/2.0) | |
launcher_angle = PI/2.0; | |
} | |
int fire = 0; | |
if (keys_pressed & P_2ND) { | |
if (!firing) { | |
firing = 1; | |
fire = 1; | |
} | |
} else { | |
firing = 0; | |
} | |
if (keys_pressed & P_DIAMOND) | |
fire = 1; | |
if (fire) { | |
ball = hpool_alloc(hp_Ball); | |
if (ball == NULL) | |
ER_throw(ER_MEMORY); | |
ball->prev = balls.prev; | |
ball->next = &balls; | |
ball->prev->next = ball; | |
ball->next->prev = ball; | |
ball->x = ((-BALL_SIZE >> 1) << SCALE_SHIFT); | |
ball->y = ((LCD_HEIGHT - BALL_SIZE) >> 1) << SCALE_SHIFT; | |
ball->dx = launcher_dx; | |
ball->dy = launcher_dy; | |
} | |
} | |
} | |
// Main Function | |
void _main(void) | |
{ | |
TRY | |
INT_HANDLER oldInt1; | |
back_buffer = HeapAllocPtr(LCD_SIZE); | |
if (back_buffer == NULL) | |
ER_throw(ER_MEMORY); | |
hp_Ball = hpool_new(sizeof(Ball)); | |
if (hp_Ball == NULL) { | |
HeapFreePtr(back_buffer); | |
ER_throw(ER_MEMORY); | |
} | |
oldInt1 = GetIntVec(AUTO_INT_1); | |
SetIntVec(AUTO_INT_1, timer_handler); | |
PortSet(back_buffer, 239, 127); | |
TRY | |
demo(); | |
FINALLY | |
PortRestore(); | |
SetIntVec(AUTO_INT_1, oldInt1); | |
hpool_delete(hp_Ball); | |
HeapFreePtr(back_buffer); | |
ENDFINAL | |
ONERR | |
ERD_process(errCode); | |
ENDTRY | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment