change length or size specifiers in APIs from int to long
add array API (http://cr.yp.to/lib/array.html)
This commit is contained in:
@@ -1,4 +1,14 @@
|
||||
#ifdef __dietlibc__
|
||||
#include <sys/cdefs.h>
|
||||
#else
|
||||
#define __likely(x) x
|
||||
#define __unlikely(x) x
|
||||
#endif
|
||||
#include <sys/types.h>
|
||||
#include <stdlib.h>
|
||||
#include "safemult.h"
|
||||
#include "array.h"
|
||||
#include "byte.h"
|
||||
|
||||
#if 0
|
||||
static array x;
|
||||
@@ -39,13 +49,32 @@
|
||||
that, use array_fail.
|
||||
#endif
|
||||
|
||||
void* array_allocate(array* x,int64 membersize,int64 pos) {
|
||||
int64 wanted;
|
||||
if (membersize<128)
|
||||
wanted=(pos+127)&(-128ll); /* round up to multiple of 128 */
|
||||
else
|
||||
wanted=(pos+4095)&(-4096ll); /* round up to 4k pages */
|
||||
/* detect numeric overflow */
|
||||
if (wanted<0) return 0;
|
||||
wanted=membersize*(pos+1);
|
||||
void* array_allocate(array* x,uint64 membersize,int64 pos) {
|
||||
uint64 wanted;
|
||||
if (__unlikely(x->allocated<0)) return 0; /* array is failed */
|
||||
if (__unlikely(pos+1<1)) return 0;
|
||||
/* second case of overflow: pos*membersize too large */
|
||||
if (__unlikely(umult64(membersize,pos+1,&wanted))) return 0;
|
||||
|
||||
if (__unlikely(wanted > x->allocated)) {
|
||||
/* round up a little */
|
||||
if (membersize<8)
|
||||
wanted=(wanted+127)&(-128ll); /* round up to multiple of 128 */
|
||||
else
|
||||
wanted=(wanted+4095)&(-4096ll); /* round up to 4k pages */
|
||||
|
||||
if (__unlikely(wanted<128)) return 0; /* overflow during rounding */
|
||||
|
||||
if (sizeof(size_t) != sizeof(int64) && __unlikely((size_t)(wanted) != wanted))
|
||||
return 0;
|
||||
{
|
||||
char* tmp=realloc(x->p,wanted);
|
||||
if (__unlikely(!tmp)) return 0;
|
||||
x->p=tmp;
|
||||
}
|
||||
x->allocated=wanted;
|
||||
byte_zero(x->p+x->initialized,x->allocated-x->initialized);
|
||||
}
|
||||
x->initialized=pos*membersize;
|
||||
return x->p+pos*membersize;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user