static void registerConsole(rlc_console c) { rlc_console *p; int n; LOCK(); for(;;) { for(p=consoles, n=0; n++<consoles_length; p++) { if ( !*p ) { *p = c; rlc_set(c, RLC_REGISTER, (uintptr_t)c, unregisterConsole); UNLOCK(); return; } } if ( consoles_length ) { int bytes = consoles_length*sizeof(rlc_console); consoles = PL_realloc(consoles, bytes*2); memset(consoles+consoles_length, 0, bytes); consoles_length *= 2; } else { consoles_length = 10; consoles = PL_malloc(consoles_length*sizeof(rlc_console)); memset(consoles, 0, consoles_length*sizeof(rlc_console)); } } }
static int add_charbuf(charbuf *cb, int c) { if ( cb->here < cb->end ) { *cb->here++ = c; } else { size_t len = (cb->end-cb->base); if ( cb->base == cb->tmp ) { pl_wchar_t *n = PL_malloc(len*2*sizeof(pl_wchar_t)); memcpy(n, cb->base, sizeof(cb->tmp)); cb->base = n; } else { cb->base = PL_realloc(cb->base, len*2*sizeof(pl_wchar_t)); } cb->here = &cb->base[len]; cb->end = &cb->base[len*2]; *cb->here++ = c; } return TRUE; }
static int add_ecbuf(ecbuf *b, echar *data, size_t len) { if ( b->size + len > b->allocated ) { size_t newsize = (b->allocated ? b->allocated * 2 : 2048); while( b->size + len > newsize ) newsize *= 2; if ( b->buffer ) { b->buffer = PL_realloc(b->buffer, newsize*sizeof(echar)); } else { b->buffer = PL_malloc(newsize*sizeof(echar)); } b->allocated = newsize; } memcpy(b->buffer+b->size, data, len*sizeof(echar)); b->size += len; return TRUE; }