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