Mirror of :pserver:cvs@cvs.fefe.de:/cvs libowfat https://www.fefe.de/libowfat/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

38 lines
1022 B

13 years ago
#include "likely.h"
#include <stdlib.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <unistd.h>
13 years ago
#include "iarray.h"
#ifdef __dietlibc__
#include <sys/atomic.h>
#define __CAS(ptr,oldval,newval) __sync_val_compare_and_swap(ptr,oldval,newval)
13 years ago
static iarray_page* new_page(size_t pagesize) {
if (x==MAP_FAILED) return 0;
return (iarray_page*)x;
13 years ago
void* iarray_allocate(iarray* ia,size_t pos) {
size_t index;
iarray_page** p=&ia->pages[pos%(sizeof(ia->pages)/sizeof(ia->pages[0]))];
iarray_page* newpage=0;
for (index=0; pos<index+ia->elemperpage; index+=ia->elemperpage) {
if (!*p) {
if (!newpage)
if (!(newpage=new_page(ia->bytesperpage))) return 0;
if (__CAS(p,0,newpage)==0)
if (index+ia->elemperpage>pos) {
if (newpage) munmap(newpage,ia->bytesperpage);
return &(*p)->data[(pos-index)*ia->elemsize];
13 years ago
return 0; // can't happen
13 years ago