void get_raw_input(t_raw *raw, t_hist *hist, int *enter, int *move) { int err; while (!*enter) { *move = 0; raw->rd = raw_alloc(sizeof(char) * 10); raw->line->oldcursor = raw->line->cursor; if (read(0, raw->rd, 10) < 0) continue ; if (raw->rd[0] > 31 && raw->rd[0] < 127) { err = insert_char(raw, raw->rd[0]); raw->complete = 0; raw->beg = NULL; } else err = get_std_escape(raw, raw->rd, enter, move); (err != SUCCESS) ? (input_error(err)) : (0); if (err == ERROR && raw->line->input->buffer[0] == 0) return ; redraw(raw, *move, *enter); free(raw->rd); } free_hist(raw->history); }
oop string_join(oop strlist, oop sepstr) { size_t total_length = 0; size_t sep_length; int needsep = 0; oop pos; binary *result; char *c; if (!isbinary(sepstr)) die("string_join: sepstr is not a binary"); sep_length = oop_len(sepstr); for (pos = strlist; ispair(pos); pos = ((pair *) pos)->cdr) { oop s = ((pair *) pos)->car; if (!isbinary(s)) die("string_join: element of strlist is not a binary"); if (needsep) total_length += sep_length; total_length += oop_len(s); needsep = 1; } if (!isnil(pos)) die("string_join: strlist is not a list"); result = raw_alloc(sizeof(binary) + total_length); init_binary_header(result->header, TYPE_BINARY, total_length); c = result->data; needsep = 0; for (pos = strlist; ispair(pos); pos = ((pair *) pos)->cdr) { oop s = ((pair *) pos)->car; if (needsep) { memcpy(c, ((binary *) sepstr)->data, sep_length); c += sep_length; } memcpy(c, ((binary *) s)->data, oop_len(s)); c += oop_len(s); needsep = 1; } return result; }
void raw_memmove(void *dest, void *src, size_t size) { void *tmp; tmp = raw_alloc(size); memcpy(tmp, src, size); memcpy(dest, tmp, size); free(tmp); }
oop string_split_by_chars(oop s, oop charstr) { oop result = mknull(); size_t limit; pair *prev = NULL; char *separators; size_t separators_len; size_t tokstart; size_t pos; char *sdata; if (!isbinary(s)) die("string_split_by_chars: string is not a binary"); if (!isbinary(charstr)) die("string_split_by_chars: separators is not a binary"); separators = ((binary *) charstr)->data; separators_len = oop_len(charstr); sdata = ((binary *) s)->data; tokstart = pos = 0; limit = oop_len(s); while (pos < limit) { /* Find the first separator */ while (pos < limit && memchr(separators, sdata[pos], separators_len) == NULL) pos++; if (tokstart != pos) { binary *tok = raw_alloc(sizeof(binary) + (pos - tokstart)); pair *p = raw_cons(tok, mknull()); init_binary_header(tok->header, TYPE_BINARY, (pos - tokstart)); memcpy(tok->data, &sdata[tokstart], pos - tokstart); if (prev == NULL) { result = p; } else { prev->cdr = p; } prev = p; } /* Find the first nonseparator */ while (pos < limit && memchr(separators, sdata[pos], separators_len) != NULL) pos++; tokstart = pos; } return result; }
// called by the next generation down to promote an // already allocated object to the generation in question. inline void *promote(void *obj, size_t size, ValueType type, bool pinned) { void *n = raw_alloc(size, type, pinned); memcpy(n, obj, size); return n; }
void *kmalloc(size_t size) { void *address = NULL; int bin_index; struct heap_page *page; unsigned long irq_state = 0; uint32 i; #if MAKE_NOIZE kprint("kmalloc: asked to allocate size %d\n", size); #endif /* lock */ HEAP_LOCK(irq_state); /* find a bin of suitable size */ for (bin_index = 0; bin_index < bin_count; bin_index++) if (size <= bins[bin_index].element_size) break; /* if requested memory block is too big, * no bin of suitable size discovered */ if (bin_index == bin_count) { /* XXX fix the raw alloc later. */ panic("kmalloc: asked to allocate too much for now!\n"); goto out; } else { /* allocate space */ if (bins[bin_index].free_list != NULL) { address = bins[bin_index].free_list; bins[bin_index].free_list = (void *)(*(addr_t *)bins[bin_index].free_list); bins[bin_index].free_count--; } else { if (bins[bin_index].raw_count == 0) { bins[bin_index].raw_list = raw_alloc(bins[bin_index].grow_size, bin_index); bins[bin_index].raw_count = bins[bin_index].grow_size / bins[bin_index].element_size; } bins[bin_index].raw_count--; address = bins[bin_index].raw_list; bins[bin_index].raw_list += bins[bin_index].element_size; } bins[bin_index].alloc_count++; page = &heap_alloc_table[((addr_t)address - heap_base) / PAGE_SIZE]; page[0].free_count--; #if MAKE_NOIZE kprint("kmalloc0: page 0x%x: bin_index %d, free_count %d\n", page, page->bin_index, page->free_count); #endif for(i = 1; i < bins[bin_index].element_size / PAGE_SIZE; i++) { page[i].free_count--; #if MAKE_NOIZE kprint("kmalloc1: page 0x%x: bin_index %d, free_count %d\n", page[i], page[i].bin_index, page[i].free_count); #endif } } out: /* unlock */ HEAP_UNLOCK(irq_state); #if MAKE_NOIZE kprint("kmalloc: asked to allocate size %d, returning ptr = %p\n", size, address); #endif return address; }