mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-07-12 14:09:12 +00:00
Remove old stack code and improve dirstream
This commit is contained in:
parent
74caabb823
commit
dc6c67256f
61 changed files with 463 additions and 595 deletions
|
@ -21,23 +21,35 @@
|
|||
#include "libc/mem/mem.h"
|
||||
#include "libc/str/str.h"
|
||||
|
||||
static void *critbit0_dup(const void *u, size_t ulen) {
|
||||
char *res;
|
||||
if ((res = malloc(ulen + 1))) {
|
||||
if (ulen) {
|
||||
memcpy(res, u, ulen);
|
||||
}
|
||||
res[ulen] = 0;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
/**
|
||||
* Inserts 𝑢 into 𝑡 without copying.
|
||||
*
|
||||
* @param t is critical bit tree
|
||||
* @param u is nul-terminated string which must be 8+ byte aligned
|
||||
* and becomes owned by the tree afterwards
|
||||
* @return true if 𝑡 was mutated, or -1 w/ errno
|
||||
* @return 1 if 𝑡 was mutated, 0 if present, or -1 w/ errno
|
||||
* @note h/t djb and agl
|
||||
*/
|
||||
int critbit0_emplace(struct critbit0 *t, char *u, size_t ulen) {
|
||||
int critbit0_emplace(struct critbit0 *t, const void *u, size_t ulen) {
|
||||
unsigned char *p = t->root;
|
||||
if (!p) {
|
||||
t->root = u;
|
||||
t->count = 1;
|
||||
return 1;
|
||||
if ((u = critbit0_dup(u, ulen))) {
|
||||
t->root = u;
|
||||
t->count = 1;
|
||||
return 1;
|
||||
} else {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
const unsigned char *const ubytes = (void *)u;
|
||||
const unsigned char *ubytes = (void *)u;
|
||||
while (1 & (intptr_t)p) {
|
||||
struct CritbitNode *q = (void *)(p - 1);
|
||||
unsigned char c = 0;
|
||||
|
@ -66,7 +78,8 @@ DifferentByteFound:
|
|||
unsigned char c = p[newbyte];
|
||||
int newdirection = (1 + (newotherbits | c)) >> 8;
|
||||
struct CritbitNode *newnode;
|
||||
if ((newnode = malloc(sizeof(struct CritbitNode)))) {
|
||||
if ((newnode = malloc(sizeof(struct CritbitNode))) &&
|
||||
(ubytes = critbit0_dup(ubytes, ulen))) {
|
||||
newnode->byte = newbyte;
|
||||
newnode->otherbits = newotherbits;
|
||||
newnode->child[1 - newdirection] = (void *)ubytes;
|
||||
|
@ -87,6 +100,7 @@ DifferentByteFound:
|
|||
t->count++;
|
||||
return 1;
|
||||
} else {
|
||||
free(newnode);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue