void *mem_acquire(void **mem_head) { if (*mem_head) { uint16_t free_count; void *head; void *mem; /* Get the free count from the list and decrement it */ free_count = *((uint16_t *)MROUND((uint8_t *)*mem_head + sizeof(mem_head))); free_count--; mem = *mem_head; memcpy(&head, mem, sizeof(head)); /* Store free mem_count after the list's next pointer */ if (head) { *((uint16_t *)MROUND((uint8_t *)head + sizeof(head))) = free_count; } *mem_head = head; return mem; } return NULL; }
void mem_init(void *mem_pool, uint16_t mem_size, uint16_t mem_count, void **mem_head) { *mem_head = mem_pool; /* Store free mem_count after the list's next pointer at an aligned * memory location to ensure atomic read/write (in ARM for now). */ *((uint16_t *)MROUND((uint8_t *)mem_pool + sizeof(mem_pool))) = mem_count; /* Initialize next pointers to form a free list, * next pointer is stored in the first 32-bit of each block */ memset(((uint8_t *)mem_pool + (mem_size * (--mem_count))), 0, sizeof(mem_pool)); while (mem_count--) { uint32_t next; next = (uint32_t)((uint8_t *) mem_pool + (mem_size * (mem_count + 1))); memcpy(((uint8_t *)mem_pool + (mem_size * mem_count)), (void *)&next, sizeof(next)); } }
void PicRender( const Pic *p, SDL_Renderer *r, const struct vec2i pos, const color_t mask, const double radians, const struct vec2 scale) { Rect2i dest = Rect2iNew(pos, p->size); // Apply scale to render dest // TODO: render with anchor at centre by default? if (!svec2_is_equal(scale, svec2_one())) { dest.Pos.x -= (mint_t)MROUND((scale.x - 1) * p->size.x / 2); dest.Pos.y -= (mint_t)MROUND((scale.y - 1) * p->size.y / 2); dest.Size.x = (mint_t)MROUND(p->size.x * scale.x); dest.Size.y = (mint_t)MROUND(p->size.y * scale.y); } const double angle = ToDegrees(radians); TextureRender(p->Tex, r, dest, mask, angle); }
void mem_release(void *mem, void **mem_head) { uint16_t free_count = 0; /* Get the free count from the list and increment it */ if (*mem_head) { free_count = *((uint16_t *)MROUND((uint8_t *)*mem_head + sizeof(mem_head))); } free_count++; memcpy(mem, mem_head, sizeof(mem)); /* Store free mem_count after the list's next pointer */ *((uint16_t *)MROUND((uint8_t *)mem + sizeof(mem))) = free_count; *mem_head = mem; }
uint16_t mem_free_count_get(void *mem_head) { uint16_t free_count = 0; /* Get the free count from the list */ if (mem_head) { free_count = *((uint16_t *)MROUND((uint8_t *)mem_head + sizeof(mem_head))); } return free_count; }
void s128_maconly(s128_ctx *c, UCHAR *buf, int nbytes) { UCHAR *endbuf; WORD t = 0; if ((nbytes & 3) != 0) abort(); endbuf = &buf[nbytes]; /* do small or odd size buffers the slow way, at least at first */ while ((nbytes % (N*4)) != 0) { cycle(c->R); macfunc(c, BYTE2WORD(buf)); buf += 4; nbytes -= 4; } /* now do lots at a time, if there's any left */ while (buf < endbuf) { MROUND(0); MROUND(1); MROUND(2); MROUND(3); MROUND(4); MROUND(5); MROUND(6); MROUND(7); MROUND(8); MROUND(9); MROUND(10); MROUND(11); MROUND(12); MROUND(13); MROUND(14); MROUND(15); MROUND(16); buf += 4*17; } }