tabs->spaces

This commit is contained in:
Nils O. Selåsdal
2013-11-22 20:06:05 +01:00
parent 3e05476694
commit 9090de0663
31 changed files with 1214 additions and 1214 deletions
+3 -3
View File
@@ -17,13 +17,13 @@ extern "C" {
typedef struct GBuf GBuf;
struct GBuf {
/** Allocated buffer length.*/
size_t len;
size_t len;
/** Used space in the buffer */
size_t used;
size_t used;
/** Reference count */
size_t ref_cnt;
/** The data */
void *buf;
void *buf;
};
/** Allocate a new GBuf.
+4 -4
View File
@@ -40,13 +40,13 @@ extern "C" {
typedef struct DBuf DBuf;
struct DBuf {
/** Start of user data */
unsigned char *start;
unsigned char *start;
/** End of user data */
unsigned char *end;
unsigned char *end;
/** Start of underlying buffer. Internal use.*/
unsigned char *bufstart;
unsigned char *bufstart;
/** End of underlying buffer. Internal use.*/
unsigned char *bufend;
unsigned char *bufend;
};
/** Free the internals of a DBuf, does not free buf itself.
* Does nothing if buf == NULL
+1 -1
View File
@@ -12,7 +12,7 @@ typedef int (*uc_hs_cmp)(const void *, const void *);
void
uc_heapsort(void *base, size_t count, size_t width,
uc_hs_cmp cmp);
uc_hs_cmp cmp);
#ifdef __cplusplus
}
+9 -9
View File
@@ -22,15 +22,15 @@ uc_phi_64(uint64_t N);
static inline uint32_t
uc_nextpow2(uint32_t x)
{
x--;
x |= x >> 1;
x |= x >> 2;
x |= x >> 4;
x |= x >> 8;
x |= x >> 16;
x++;
return x;
x--;
x |= x >> 1;
x |= x >> 2;
x |= x >> 4;
x |= x >> 8;
x |= x >> 16;
x++;
return x;
}
#ifdef __cplusplus
+9 -9
View File
@@ -7,23 +7,23 @@ extern "C" {
typedef enum RBColor RBColor;
enum RBColor {
Red,
Black
Red,
Black
};
typedef struct RBNode RBNode;
struct RBNode {
unsigned char color; //Red/Black
RBNode *parent;
RBNode *right;
RBNode *left;
void *data;
unsigned char color; //Red/Black
RBNode *parent;
RBNode *right;
RBNode *left;
void *data;
};
typedef struct RBTree RBTree;
struct RBTree {
RBNode *node;
int (*compare)(const RBNode *a, const RBNode *b);
RBNode *node;
int (*compare)(const RBNode *a, const RBNode *b);
};
/** Remove a node from the tree.
+10 -10
View File
@@ -26,13 +26,13 @@ extern "C" {
*
*/
struct uc_threadmsg{
/**
* A message type the application can use to
/**
* A message type the application can use to
* discriminate different messages.
* A negative value will cause a uc_threadmsg to
* be added to the head of a queue.
*/
long msgtype;
*/
long msgtype;
/**
* Internal pointer used by the uc_thread_queue.
@@ -100,22 +100,22 @@ struct uc_threadqueue {
* Number of elements in the queue.
* Use #threadqueue_length to read it.
*/
unsigned int num_elements;
unsigned int num_elements;
/** Max number of elements this queue will hold */
unsigned int max_elements;
/**
* Mutex for the queue.
*/
pthread_mutex_t mutex;
pthread_mutex_t mutex;
/**
* Condition variable for readers on the queue.
*/
pthread_cond_t read_cond;
pthread_cond_t read_cond;
/**
* Condition variable for writers on the queue (if the queue is full).
*/
pthread_cond_t write_cond;
pthread_cond_t write_cond;
/**
* Number of threads blocking on writing to the queue
*/
@@ -128,7 +128,7 @@ struct uc_threadqueue {
/**
* Internal pointers for the messages in the queue.
*/
struct uc_threadmsg *first,**last;
struct uc_threadmsg *first,**last;
};
/**
@@ -192,7 +192,7 @@ static inline int uc_thread_queue_add(struct uc_threadqueue *queue, struct uc_th
*
* struct timespec is defined as:
* @code
* struct timespec {
* struct timespec {
* long tv_sec; // seconds
* long tv_nsec; // nanoseconds
* };
+622 -622
View File
File diff suppressed because it is too large Load Diff
+2 -2
View File
@@ -31,7 +31,7 @@ uc_ascii2bcd(const char *ascii, unsigned char *bcdout, unsigned char filler)
bcdp++;
}
return bcdp - bcdout;
return bcdp - bcdout;
}
static const char hex[] = "0123456789ABCDEF";
@@ -48,6 +48,6 @@ uc_bcd2ascii(const unsigned char *bcd, size_t bcdlen, char *asciiout)
asciiout[idx] = 0;
return idx;
return idx;
}
+33 -33
View File
@@ -6,23 +6,23 @@
GBuf*
uc_new_gbuf(size_t initsz)
{
GBuf *p;
p = malloc(sizeof *p);
if (p == NULL)
return NULL;
{
GBuf *p;
p = malloc(sizeof *p);
if (p == NULL)
return NULL;
p->used = 0;
p->len = initsz;
p->buf = NULL;
p->used = 0;
p->len = initsz;
p->buf = NULL;
p->ref_cnt = 1;
if (initsz != 0 && (p->buf = malloc(initsz)) == NULL) {
free(p);
return NULL;
}
return p;
if (initsz != 0 && (p->buf = malloc(initsz)) == NULL) {
free(p);
return NULL;
}
return p;
}
void
@@ -45,17 +45,17 @@ uc_gbuf_unref(GBuf *buf)
int
uc_gbuf_append(GBuf *buf,const void *data,size_t len)
{
unsigned char *gbuf;
if (buf->len - buf->used < len)
if (uc_gbuf_grow(buf, len + len/2))
return -1;
unsigned char *gbuf;
if (buf->len - buf->used < len)
if (uc_gbuf_grow(buf, len + len/2))
return -1;
gbuf = buf->buf;
memcpy(gbuf + buf->used, data, len);
buf->used += len;
return 0;
gbuf = buf->buf;
memcpy(gbuf + buf->used, data, len);
buf->used += len;
return 0;
}
int uc_gbuf_printf(GBuf *buf, const char *fmt, ...)
@@ -102,18 +102,18 @@ int uc_gbuf_printf(GBuf *buf, const char *fmt, ...)
int
uc_gbuf_grow(GBuf *buf, size_t addlen)
{
void *tmp;
size_t newsz;
void *tmp;
size_t newsz;
newsz = buf->len + addlen;
newsz = buf->len + addlen;
tmp = realloc(buf->buf,newsz);
if (tmp == NULL)
return -1;
tmp = realloc(buf->buf,newsz);
if (tmp == NULL)
return -1;
buf->buf = tmp;
buf->len = newsz;
buf->buf = tmp;
buf->len = newsz;
return 0;
return 0;
}
+8 -8
View File
@@ -57,15 +57,15 @@ init_crc32()
#include <stdio.h>
int main()
{
int i;
init_crc32();
for(i = 0; i < 256; i++) {
printf("0x%X,",crc32_table[i]);
if(i%10 == 0)
putchar('\n');
}
int i;
init_crc32();
for(i = 0; i < 256; i++) {
printf("0x%X,",crc32_table[i]);
if(i%10 == 0)
putchar('\n');
}
return 0;
return 0;
}
*/
+12 -12
View File
@@ -1,18 +1,18 @@
int
uc_day_of_week(int day, int month, int year)
{
int cent;
int cent;
/* adjust months so February is the last one */
month -= 2;
if (month < 1) {
month += 12;
--year;
}
/* split by century */
cent = year / 100;
year %= 100;
return ((26 * month - 2) / 10 + day + year
+ year / 4 + cent / 4 + 5 * cent) % 7;
/* adjust months so February is the last one */
month -= 2;
if (month < 1) {
month += 12;
--year;
}
/* split by century */
cent = year / 100;
year %= 100;
return ((26 * month - 2) / 10 + day + year
+ year / 4 + cent / 4 + 5 * cent) % 7;
}
+5 -5
View File
@@ -3,11 +3,11 @@
uint32_t
uc_djbhash(const char *str)
{
uint32_t hash = 5381;
int c;
while ((c = *str++))
hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
uint32_t hash = 5381;
int c;
while ((c = *str++))
hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
return hash;
return hash;
}
+11 -11
View File
@@ -3,18 +3,18 @@
uint32_t
uc_elfhash(const char *str, uint32_t len)
{
uint32_t hash = 0;
uint32_t x = 0;
uint32_t i = 0;
uint32_t hash = 0;
uint32_t x = 0;
uint32_t i = 0;
for (i = 0; i < len; str++, i++) {
hash = (hash << 4) + (*str);
if ((x = hash & 0xF0000000L) != 0) {
hash ^= (x >> 24);
hash &= ~x;
}
}
for (i = 0; i < len; str++, i++) {
hash = (hash << 4) + (*str);
if ((x = hash & 0xF0000000L) != 0) {
hash ^= (x >> 24);
hash &= ~x;
}
}
return hash;
return hash;
}
+6 -6
View File
@@ -3,11 +3,11 @@
uint32_t
uc_gcd_32(uint32_t a, uint32_t b)
{
while (b != 0) {
uint32_t t = b;
b = a%b;
a = t;
}
while (b != 0) {
uint32_t t = b;
b = a%b;
a = t;
}
return a;
return a;
}
+6 -6
View File
@@ -3,11 +3,11 @@
uint64_t
uc_gcd_64(uint64_t a, uint64_t b)
{
while (b != 0) {
uint64_t t = b;
b = a%b;
a = t;
}
while (b != 0) {
uint64_t t = b;
b = a%b;
a = t;
}
return a;
return a;
}
+24 -24
View File
@@ -5,37 +5,37 @@ static void
uc_sift(unsigned char *base, size_t start, size_t count, size_t width,
uc_hs_cmp cmp)
{
size_t root = start, child;
size_t root = start, child;
while ((root * 2 + 1) < count) {
child = root * 2 + 1;
if (child < (count - 1)
&& cmp(&base[child * width], &base[(child + 1) * width]) < 0)
child++;
while ((root * 2 + 1) < count) {
child = root * 2 + 1;
if (child < (count - 1)
&& cmp(&base[child * width], &base[(child + 1) * width]) < 0)
child++;
if (cmp(&base[root * width], &base[child * width]) < 0) {
memcpy(base + root * width, base + child * width, width);
root = child;
} else
return;
}
if (cmp(&base[root * width], &base[child * width]) < 0) {
memcpy(base + root * width, base + child * width, width);
root = child;
} else
return;
}
}
void
uc_heapsort(void *base_, size_t count, size_t width,
uc_hs_cmp cmp)
uc_hs_cmp cmp)
{
int start = count / 2 - 1, end = count - 1;
unsigned char *base = base_;
int start = count / 2 - 1, end = count - 1;
unsigned char *base = base_;
while (start >= 0) {
uc_sift(base, start, count, width, cmp);
start--;
}
while (start >= 0) {
uc_sift(base, start, count, width, cmp);
start--;
}
while (end > 0) {
memcpy(base + end * width, base, width);
uc_sift(base, 0, end, width, cmp);
end--;
}
while (end > 0) {
memcpy(base + end * width, base, width);
uc_sift(base, 0, end, width, cmp);
end--;
}
}
+43 -43
View File
@@ -6,71 +6,71 @@
/*Beware , pollution. But the names are from the spec */
enum {
N = MT_RAND_N,
w = 32,
M = 387,
R = 19,
U = 0x80000000,
LL= 0x7FFFFFFF,
a = 0x9908B0DF,
s = 7,
t = 15,
b = 0x9D2C5680,
c = 0xEFC60000,
l = 18,
u = 11,
N = MT_RAND_N,
w = 32,
M = 387,
R = 19,
U = 0x80000000,
LL= 0x7FFFFFFF,
a = 0x9908B0DF,
s = 7,
t = 15,
b = 0x9D2C5680,
c = 0xEFC60000,
l = 18,
u = 11,
};
void mtsrand(int seed, MTRand *r)
{
int j;
for (j = 0 ; j < N ; j++) {
r->x[j] = seed * ((j+1)<< 3)|0x1;
}
int j;
for (j = 0 ; j < N ; j++) {
r->x[j] = seed * ((j+1)<< 3)|0x1;
}
r->i = 0;
}
unsigned int mtrand(MTRand *r)
{
unsigned int y;
unsigned int ch[] = {0, a};
y = r->x[r->i] & U;
y |= r->x[r->i % N];
y &= LL;
r->x[r->i] = r->x[(r->i + M ) %N];
r->x[r->i] ^= y >> 1;
unsigned int y;
unsigned int ch[] = {0, a};
y = r->x[r->i] & U;
y |= r->x[r->i % N];
y &= LL;
r->x[r->i] = r->x[(r->i + M ) %N];
r->x[r->i] ^= y >> 1;
r->x[r->i] ^= ch[y&0x1];
y = r->x[r->i];
y ^= y >> u;
y ^= (y << s) & b;
y ^= (y << t) & c;
y ^= y >> l;
r->x[r->i] ^= ch[y&0x1];
y = r->x[r->i];
y ^= y >> u;
y ^= (y << s) & b;
y ^= (y << t) & c;
y ^= y >> l;
r->i = (r->i + 1) % N;
r->i = (r->i + 1) % N;
return y;
}
return y;
}
#ifdef TEST_MT
#include <stdio.h>
#include <time.h>
int main(int argc,char *argv[])
{
MTRand r;
mtsrand(time(NULL),&r);
for (;;){
unsigned int _y = mtrand(&r);
mtsrand(time(NULL),&r);
for (;;){
unsigned int _y = mtrand(&r);
if (_y < 100)
printf("%u \n",_y);
}
if (_y < 100)
printf("%u \n",_y);
}
return 0;
return 0;
}
#endif
+5 -5
View File
@@ -7,14 +7,14 @@ phi(N) = how many numbers between 1 and N - 1 which are relatively prime to N.
uint32_t
uc_phi_32(uint32_t N)
{
uint32_t phi = 1;
uint32_t i;
uint32_t phi = 1;
uint32_t i;
for (i = 2 ; i < N ; ++i){
if (uc_gcd_32(i, N) == 1){
++phi;
}
}
}
}
return phi;
return phi;
}
+5 -5
View File
@@ -7,14 +7,14 @@ phi(N) = how many numbers between 1 and N - 1 which are relatively prime to N.
uint64_t
uc_phi_64(uint64_t N)
{
uint64_t phi = 1;
uint64_t i;
uint64_t phi = 1;
uint64_t i;
for (i = 2 ; i < N ; ++i){
if (uc_gcd_64(i, N) == 1){
++phi;
}
}
}
}
return phi;
return phi;
}
+9 -9
View File
@@ -4,16 +4,16 @@ uint32_t
uc_pjwhash(const char *str,size_t len)
{
unsigned h = 0, g;
unsigned h = 0, g;
while (len--) {
h = (h << 4) + *str++;
if ((g = h & 0xf0000000)) {
h ^= (g >> 24);
h ^= g;
}
}
while (len--) {
h = (h << 4) + *str++;
if ((g = h & 0xf0000000)) {
h ^= (g >> 24);
h ^= g;
}
}
return h;
return h;
}
+228 -228
View File
@@ -5,290 +5,290 @@
static void
rotateleft(RBTree *root, RBNode *node)
{
RBNode *right = node->right;
RBNode *right = node->right;
node->right = right->left;
if (right->left)
right->left->parent = node;
right->left = node;
node->right = right->left;
if (right->left)
right->left->parent = node;
right->left = node;
right->parent = node->parent;
if (node->parent) {
if (node == node->parent->left)
node->parent->left = right;
else
node->parent->right = right;
} else
root->node = right;
node->parent = right;
right->parent = node->parent;
if (node->parent) {
if (node == node->parent->left)
node->parent->left = right;
else
node->parent->right = right;
} else
root->node = right;
node->parent = right;
}
static void
rotateright(RBTree *root, RBNode *node)
{
RBNode *left = node->left;
RBNode *left = node->left;
node->left = left->right;
if (left->right)
left->right->parent = node;
left->right = node;
node->left = left->right;
if (left->right)
left->right->parent = node;
left->right = node;
left->parent = node->parent;
if (node->parent) {
if (node == node->parent->right)
node->parent->right = left;
else
node->parent->left = left;
} else
root->node = left;
node->parent = left;
left->parent = node->parent;
if (node->parent) {
if (node == node->parent->right)
node->parent->right = left;
else
node->parent->left = left;
} else
root->node = left;
node->parent = left;
}
static void
rb_insertcolor(RBTree *root, RBNode *node)
{
RBNode *parent, *gparent;
RBNode *parent, *gparent;
while ((parent = node->parent) && parent->color == Red) {
gparent = parent->parent;
while ((parent = node->parent) && parent->color == Red) {
gparent = parent->parent;
if (parent == gparent->left) {
RBNode *uncle = gparent->right;
if (uncle && uncle->color == Red) {
uncle->color = Black;
parent->color = Black;
gparent->color = Red;
node = gparent;
continue;
}
if (parent == gparent->left) {
RBNode *uncle = gparent->right;
if (uncle && uncle->color == Red) {
uncle->color = Black;
parent->color = Black;
gparent->color = Red;
node = gparent;
continue;
}
if (parent->right == node) {
RBNode *tmp;
rotateleft(root, parent);
tmp = parent;
parent = node;
node = tmp;
}
if (parent->right == node) {
RBNode *tmp;
rotateleft(root, parent);
tmp = parent;
parent = node;
node = tmp;
}
parent->color = Black;
gparent->color = Red;
rotateright(root, gparent);
}
parent->color = Black;
gparent->color = Red;
rotateright(root, gparent);
}
else {
RBNode *uncle = gparent->left;
if (uncle && uncle->color == Red) {
uncle->color = Black;
parent->color = Black;
gparent->color = Red;
node = gparent;
continue;
}
else {
RBNode *uncle = gparent->left;
if (uncle && uncle->color == Red) {
uncle->color = Black;
parent->color = Black;
gparent->color = Red;
node = gparent;
continue;
}
if (parent->left == node) {
RBNode *tmp;
rotateright(root, parent);
tmp = parent;
parent = node;
node = tmp;
}
if (parent->left == node) {
RBNode *tmp;
rotateright(root, parent);
tmp = parent;
parent = node;
node = tmp;
}
parent->color = Black;
gparent->color = Red;
rotateleft(root, gparent);
}
}
parent->color = Black;
gparent->color = Red;
rotateleft(root, gparent);
}
}
root->node->color = Black;
root->node->color = Black;
}
static void
removecolor(RBTree *root, RBNode *node, RBNode *parent)
{
RBNode *other;
RBNode *other;
while ((!node || node->color == Black) && node != root->node) {
if (parent->left == node) {
other = parent->right;
if (other->color == Red) {
other->color = Black;
parent->color = Red;
rotateleft(root, parent);
other = parent->right;
}
if ((!other->left || other->left->color == Black)
&& (!other->right
|| other->right->color == Black)) {
other->color = Red;
node = parent;
parent = node->parent;
} else {
if (!other->right
|| other->right->color == Black) {
RBNode *o_left;
if ((o_left = other->left))
o_left->color = Black;
other->color = Red;
rotateright(root, other);
other = parent->right;
}
other->color = parent->color;
parent->color = Black;
if (other->right)
other->right->color = Black;
rotateleft(root, parent);
node = root->node;
break;
}
}
while ((!node || node->color == Black) && node != root->node) {
if (parent->left == node) {
other = parent->right;
if (other->color == Red) {
other->color = Black;
parent->color = Red;
rotateleft(root, parent);
other = parent->right;
}
if ((!other->left || other->left->color == Black)
&& (!other->right
|| other->right->color == Black)) {
other->color = Red;
node = parent;
parent = node->parent;
} else {
if (!other->right
|| other->right->color == Black) {
RBNode *o_left;
if ((o_left = other->left))
o_left->color = Black;
other->color = Red;
rotateright(root, other);
other = parent->right;
}
other->color = parent->color;
parent->color = Black;
if (other->right)
other->right->color = Black;
rotateleft(root, parent);
node = root->node;
break;
}
}
else {
other = parent->left;
if (other->color == Red) {
other->color = Black;
parent->color = Red;
rotateright(root, parent);
other = parent->left;
}
if ((!other->left || other->left->color == Black)
&& (!other->right
|| other->right->color == Black)) {
other->color = Red;
node = parent;
parent = node->parent;
} else {
if (!other->left
|| other->left->color == Black) {
RBNode *o_right;
if ((o_right = other->right))
o_right->color = Black;
other->color = Red;
rotateleft(root, other);
other = parent->left;
}
other->color = parent->color;
parent->color = Black;
if (other->left)
other->left->color = Black;
rotateright(root, parent);
node = root->node;
break;
}
}
}
else {
other = parent->left;
if (other->color == Red) {
other->color = Black;
parent->color = Red;
rotateright(root, parent);
other = parent->left;
}
if ((!other->left || other->left->color == Black)
&& (!other->right
|| other->right->color == Black)) {
other->color = Red;
node = parent;
parent = node->parent;
} else {
if (!other->left
|| other->left->color == Black) {
RBNode *o_right;
if ((o_right = other->right))
o_right->color = Black;
other->color = Red;
rotateleft(root, other);
other = parent->left;
}
other->color = parent->color;
parent->color = Black;
if (other->left)
other->left->color = Black;
rotateright(root, parent);
node = root->node;
break;
}
}
}
if (node)
node->color = Black;
if (node)
node->color = Black;
}
void
uc_rb_remove(RBTree *root, RBNode *node)
{
RBNode *child, *parent;
int color;
RBNode *child, *parent;
int color;
if (!node->left)
child = node->right;
else if (!node->right)
child = node->left;
else {
RBNode *old = node, *left;
if (!node->left)
child = node->right;
else if (!node->right)
child = node->left;
else {
RBNode *old = node, *left;
node = node->right;
while ((left = node->left))
node = left;
child = node->right;
parent = node->parent;
color = node->color;
node = node->right;
while ((left = node->left))
node = left;
child = node->right;
parent = node->parent;
color = node->color;
if (child)
child->parent = parent;
if (parent) {
if (parent->left == node)
parent->left = child;
else
parent->right = child;
} else
root->node = child;
if (child)
child->parent = parent;
if (parent) {
if (parent->left == node)
parent->left = child;
else
parent->right = child;
} else
root->node = child;
if (node->parent == old)
parent = node;
node->parent = old->parent;
node->color = old->color;
node->right = old->right;
node->left = old->left;
if (node->parent == old)
parent = node;
node->parent = old->parent;
node->color = old->color;
node->right = old->right;
node->left = old->left;
if (old->parent) {
if (old->parent->left == old)
old->parent->left = node;
else
old->parent->right = node;
} else
root->node = node;
if (old->parent) {
if (old->parent->left == old)
old->parent->left = node;
else
old->parent->right = node;
} else
root->node = node;
old->left->parent = node;
if (old->right)
old->right->parent = node;
goto finish;
}
old->left->parent = node;
if (old->right)
old->right->parent = node;
goto finish;
}
parent = node->parent;
color = node->color;
parent = node->parent;
color = node->color;
if (child)
child->parent = parent;
if (parent) {
if (parent->left == node)
parent->left = child;
else
parent->right = child;
} else
root->node = child;
if (child)
child->parent = parent;
if (parent) {
if (parent->left == node)
parent->left = child;
else
parent->right = child;
} else
root->node = child;
finish:
if (color == Black)
removecolor(root, child, parent);
if (color == Black)
removecolor(root, child, parent);
}
RBNode*
uc_rb_find(RBTree *root, const RBNode *val)
{
RBNode *n = root->node;
while (n) {
int d = root->compare(val,n);
if (d < 0)
n = n->left;
else if (d > 0)
n = n->right;
else
return n;
}
return NULL;
RBNode *n = root->node;
while (n) {
int d = root->compare(val,n);
if (d < 0)
n = n->left;
else if (d > 0)
n = n->right;
else
return n;
}
return NULL;
}
RBNode*
uc_rb_insert(RBTree *root, RBNode *child)
{
RBNode **p = &root->node;
RBNode *parent = NULL;
int d;
while (*p) {
parent = *p;
d = root->compare(child, parent);
if (d < 0)
p = &(*p)->left;
else if (d > 0)
p = &(*p)->right;
else
return parent;
}
RBNode **p = &root->node;
RBNode *parent = NULL;
int d;
while (*p) {
parent = *p;
d = root->compare(child, parent);
if (d < 0)
p = &(*p)->left;
else if (d > 0)
p = &(*p)->right;
else
return parent;
}
child->parent = parent;
child->color = Red;
child->left = child->right = NULL;
*p = child;
rb_insertcolor(root, child);
return NULL;
child->parent = parent;
child->color = Red;
child->left = child->right = NULL;
*p = child;
rb_insertcolor(root, child);
return NULL;
}
RBNode *uc_rb_first(const RBTree *root)
+18 -18
View File
@@ -1,9 +1,9 @@
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "ucore/read_file.h"
#define CHUNK_SZ 1024
@@ -11,34 +11,34 @@
char *
uc_read_file(const char *f, size_t *length, size_t max)
{
int fd;
char *s = NULL;
char *p;
struct stat statb;
int fd;
char *s = NULL;
char *p;
struct stat statb;
size_t alloc_sz;
*length = 0;
if ((fd = open(f, O_RDONLY)) == -1)
return NULL;
if ((fd = open(f, O_RDONLY)) == -1)
return NULL;
if (fstat(fd, &statb) == -1) {
if (fstat(fd, &statb) == -1) {
goto out_close;
}
if ((statb.st_mode & S_IFMT) == S_IFREG) {
if ((statb.st_mode & S_IFMT) == S_IFREG) {
if ((size_t)statb.st_size > max) {
errno = EMSGSIZE;
goto out_close;
}
alloc_sz = (size_t) statb.st_size + 1; // + 1 to avoid one realloc
s = malloc((size_t) alloc_sz + 1); //+1 for nul terminator
} else {
s = malloc((size_t) alloc_sz + 1); //+1 for nul terminator
} else {
alloc_sz = CHUNK_SZ;
s = malloc(alloc_sz + 1); //+1 for nul terminator
}
if (s == NULL) {
if (s == NULL) {
goto out_close;
}
@@ -77,6 +77,6 @@ out_err_free:
s = NULL;
out_close:
close(fd);
return s;
return s;
}
+59 -59
View File
@@ -6,8 +6,8 @@
typedef struct Chunk Chunk;
struct Chunk {
Chunk *next;
size_t firstfree; //index to the first free byte in this chunk
Chunk *next;
size_t firstfree; //index to the first free byte in this chunk
union data { //for alignment
long long ll;
void *p;
@@ -19,99 +19,99 @@ struct Chunk {
};
struct SAlloc {
size_t chunksz; //data size of each chunk
Chunk *first; //head of the chunk list
Chunk *current;
size_t chunksz; //data size of each chunk
Chunk *first; //head of the chunk list
Chunk *current;
};
static Chunk *
add_chunk(SAlloc *p)
{
Chunk *newchunk;
Chunk *newchunk;
newchunk = malloc(sizeof *newchunk + p->chunksz - (sizeof *newchunk - offsetof(Chunk, data.data)));
if (newchunk == NULL)
return NULL;
newchunk = malloc(sizeof *newchunk + p->chunksz - (sizeof *newchunk - offsetof(Chunk, data.data)));
if (newchunk == NULL)
return NULL;
if (p->current != NULL)
p->current->next = newchunk;
if (p->current != NULL)
p->current->next = newchunk;
newchunk->next = NULL;
newchunk->firstfree = 0;
newchunk->next = NULL;
newchunk->firstfree = 0;
return newchunk;
return newchunk;
}
static Chunk *
next_chunk(SAlloc *p)
{
Chunk *newchunk;
Chunk *newchunk;
if (p->current->next) {
newchunk = p->current->next;
newchunk->firstfree = 0;
} else
newchunk = add_chunk(p);
if (p->current->next) {
newchunk = p->current->next;
newchunk->firstfree = 0;
} else
newchunk = add_chunk(p);
if (newchunk)
p->current = newchunk;
if (newchunk)
p->current = newchunk;
return newchunk;
return newchunk;
}
SAlloc *
uc_new_salloc(size_t chunksz)
{
SAlloc *p;
Chunk *first;
p = malloc(sizeof *p);
if (p == NULL)
return NULL;
SAlloc *p;
Chunk *first;
p = malloc(sizeof *p);
if (p == NULL)
return NULL;
p->chunksz = chunksz;
p->current = NULL;
p->chunksz = chunksz;
p->current = NULL;
if ((first = add_chunk(p)) == NULL) {
free(p);
return NULL;
}
p->first = p->current = first;
if ((first = add_chunk(p)) == NULL) {
free(p);
return NULL;
}
p->first = p->current = first;
return p;
return p;
}
void
uc_reset_salloc(SAlloc *p)
{
Chunk *tmp;
Chunk *tmp;
for(tmp = p->first; tmp; tmp = tmp->next)
tmp->firstfree = 0;
for(tmp = p->first; tmp; tmp = tmp->next)
tmp->firstfree = 0;
p->current = p->first;
p->current = p->first;
}
void *
uc_s_alloc(SAlloc *p,size_t sz)
{
Chunk *chunk;
void *newmem;
chunk = p->current;
if (chunk->firstfree + sz > p->chunksz) {
if (sz > p->chunksz)
return NULL;
Chunk *chunk;
void *newmem;
chunk = p->current;
if (chunk->firstfree + sz > p->chunksz) {
if (sz > p->chunksz)
return NULL;
chunk = next_chunk(p);
if (chunk == NULL)
return NULL;
}
newmem = &chunk->data.data[chunk->firstfree];
chunk->firstfree += sz;
chunk = next_chunk(p);
if (chunk == NULL)
return NULL;
}
newmem = &chunk->data.data[chunk->firstfree];
chunk->firstfree += sz;
return newmem;
return newmem;
}
void *
@@ -132,15 +132,15 @@ free_chunks(Chunk *chunk)
for (next = NULL; chunk != NULL; chunk = next) {
next = chunk->next;
free(chunk);
free(chunk);
}
}
void
uc_free_salloc(SAlloc *p)
{
free_chunks(p->first);
free(p);
free_chunks(p->first);
free(p);
}
+5 -5
View File
@@ -1,12 +1,12 @@
unsigned long
uc_sdbmhash(const unsigned char *str)
{
unsigned long hash = 0;
unsigned int c;
unsigned long hash = 0;
unsigned int c;
while ((c = *str++))
hash = c + (hash << 6) + (hash << 16) - hash;
while ((c = *str++))
hash = c + (hash << 6) + (hash << 16) - hash;
return hash;
return hash;
}
+10 -10
View File
@@ -6,16 +6,16 @@
char*
uc_sprintb(char *result, unsigned int value)
{
char *s = result;
unsigned mask = ~((unsigned) (~0) >> 1);
char *s = result;
unsigned mask = ~((unsigned) (~0) >> 1);
if (s) {
while (mask) {
*s++ = (mask & value) ? '1' : '0';
mask >>= 1;
}
*s = '\0';
}
if (s) {
while (mask) {
*s++ = (mask & value) ? '1' : '0';
mask >>= 1;
}
*s = '\0';
}
return result;
return result;
}
+10 -10
View File
@@ -5,17 +5,17 @@
char*
uc_sprintbc(char *result, unsigned char value)
{
char *s = result;
unsigned char mask = ~((unsigned char) (~0U) >> 1U);
char *s = result;
unsigned char mask = ~((unsigned char) (~0U) >> 1U);
if (s) {
while (mask) {
*s++ = (mask & value) ? '1' : '0';
mask >>= 1U;
}
*s = '\0';
}
if (s) {
while (mask) {
*s++ = (mask & value) ? '1' : '0';
mask >>= 1U;
}
*s = '\0';
}
return result;
return result;
}
+10 -10
View File
@@ -5,16 +5,16 @@
char*
uc_sprintbll(char *result, unsigned long long value)
{
char *s = result;
unsigned long long mask = ~((unsigned long long) (~0) >> 1);
char *s = result;
unsigned long long mask = ~((unsigned long long) (~0) >> 1);
if (s) {
while (mask) {
*s++ = (mask & value) ? '1' : '0';
mask >>= 1;
}
*s = '\0';
}
if (s) {
while (mask) {
*s++ = (mask & value) ? '1' : '0';
mask >>= 1;
}
*s = '\0';
}
return result;
return result;
}
+10 -10
View File
@@ -5,16 +5,16 @@
char*
uc_sprintbs(char *result, unsigned short value)
{
char *s = result;
unsigned short mask = ~((unsigned short) (~0) >> 1);
char *s = result;
unsigned short mask = ~((unsigned short) (~0) >> 1);
if (s) {
while (mask) {
*s++ = (mask & value) ? '1' : '0';
mask >>= 1;
}
*s = '\0';
}
if (s) {
while (mask) {
*s++ = (mask & value) ? '1' : '0';
mask >>= 1;
}
*s = '\0';
}
return result;
return result;
}
+12 -12
View File
@@ -114,13 +114,13 @@ int uc_thread_queue_tryadd(struct uc_threadqueue *queue,
queue->num_elements++;
//signal blocked readers
if (queue->num_read_waiters == 1) {
if (queue->num_read_waiters == 1) {
//if there's just one thread waiting to pop elements
//use _cond_signal. _cond_broadcast can be more expensive (os dependent)
pthread_cond_signal(&queue->read_cond);
} else if (queue->num_read_waiters > 1) {
pthread_cond_signal(&queue->read_cond);
} else if (queue->num_read_waiters > 1) {
//signall all threads that there's items available.
pthread_cond_broadcast(&queue->read_cond);
pthread_cond_broadcast(&queue->read_cond);
}
@@ -183,13 +183,13 @@ int uc_thread_queue_tryget(struct uc_threadqueue *queue,
queue->num_elements--;
//signal blocked writers
if (queue->num_write_waiters == 1) {
if (queue->num_write_waiters == 1) {
//if there's just one thread waiting to push elements
//use _cond_signal. _cond_broadcast can be more expensive (os dependent)
pthread_cond_signal(&queue->write_cond);
} else if (queue->num_write_waiters > 1) {
pthread_cond_signal(&queue->write_cond);
} else if (queue->num_write_waiters > 1) {
//signall all threads that there's items available.
pthread_cond_broadcast(&queue->write_cond);
pthread_cond_broadcast(&queue->write_cond);
}
pthread_mutex_unlock(&queue->mutex);
@@ -301,13 +301,13 @@ int uc_thread_queue_clear(struct uc_threadqueue *queue,
queue->last = &queue->first;
//notify writes, there should be space now
if (queue->num_write_waiters == 1) {
if (queue->num_write_waiters == 1) {
//if there's just one thread waiting to push elements
//use _cond_signal. _cond_broadcast can be more expensive (os dependent)
pthread_cond_signal(&queue->write_cond);
} else if (queue->num_write_waiters > 1) {
pthread_cond_signal(&queue->write_cond);
} else if (queue->num_write_waiters > 1) {
//signall all threads that there's items available.
pthread_cond_broadcast(&queue->write_cond);
pthread_cond_broadcast(&queue->write_cond);
}
pthread_mutex_unlock(&queue->mutex);
+16 -16
View File
@@ -3,29 +3,29 @@
struct timeval *
uc_tvadd(struct timeval *dst, const struct timeval *a, const struct timeval *b)
{
dst->tv_sec = a->tv_sec + b->tv_sec;
dst->tv_usec = a->tv_usec + b->tv_usec;
dst->tv_sec = a->tv_sec + b->tv_sec;
dst->tv_usec = a->tv_usec + b->tv_usec;
if (dst->tv_usec >= 1000000) {
dst->tv_sec++;
if (dst->tv_usec >= 1000000) {
dst->tv_sec++;
dst->tv_usec -= 1000000;
}
return dst;
return dst;
}
struct timeval *
uc_tvsub(struct timeval *dst, const struct timeval *a, const struct timeval *b)
{
dst->tv_sec = a->tv_sec - b->tv_sec;
dst->tv_usec = a->tv_usec - b->tv_usec;
dst->tv_sec = a->tv_sec - b->tv_sec;
dst->tv_usec = a->tv_usec - b->tv_usec;
if (dst->tv_usec < 0) {
dst->tv_sec--;
if (dst->tv_usec < 0) {
dst->tv_sec--;
dst->tv_usec += 1000000;
}
return dst;
return dst;
}
int
@@ -36,9 +36,9 @@ uc_tvcmp(const struct timeval *a, const struct timeval *b)
else if (a->tv_sec > b->tv_sec)
return 1;
if (a->tv_usec < b->tv_usec)
return -1;
else if (a->tv_usec > b->tv_usec)
if (a->tv_usec < b->tv_usec)
return -1;
else if (a->tv_usec > b->tv_usec)
return 1;
return 0;
@@ -47,9 +47,9 @@ uc_tvcmp(const struct timeval *a, const struct timeval *b)
struct timeval *
uc_ms2tv(struct timeval *dst, unsigned long long ms)
{
dst->tv_sec = ms / 1000;
dst->tv_usec = ms % 1000 * 1000;
dst->tv_sec = ms / 1000;
dst->tv_usec = ms % 1000 * 1000;
return dst;
return dst;
}
+9 -9
View File
@@ -4,14 +4,14 @@
int main(int argc, char *argv[])
{
unsigned int a,N;
if (argc < 2) {
printf("Usage %s interger1\n",argv[0]);
return 1;
}
a = atoi(argv[1]);
N = uc_phi_32(a);
printf("phi of %u is %u\n",a,N);
unsigned int a,N;
if (argc < 2) {
printf("Usage %s interger1\n",argv[0]);
return 1;
}
a = atoi(argv[1]);
N = uc_phi_32(a);
printf("phi of %u is %u\n",a,N);
return 0;
return 0;
}