Пример #1
0
// hash and array may be included in one table at the same time
// keep array in order
// if an array contains nil value, such as {1,nil, nil, 2}, the result keeps that
static int SerializeMixData(lua_State *L)
{
	rbuf_t buf;
	rbuf_t *pbuf = &buf;
	rbuf_init(pbuf);
	int isShowCommentIdx = 0;
	int isInLine = 1;
	int argc = lua_gettop(L);
	if (argc > 3) {
		luaL_error(L, "SerializeMixData args:(value, isShowComment, InLine)");
	}
	if (argc >= 2) {
		if ( argc == 3) {
			isShowCommentIdx = (int)lua_toboolean(L, -1); // L : value, isInLine, isShowCommentIdx
			lua_pop(L, 1);
		}
		isInLine = (int)lua_toboolean(L, -1); // L : value, isInLine
		if (isInLine) {
			isShowCommentIdx = 0;
		}
		lua_pop(L, 1);
	}
	if (!ConvMixData(L, 0, pbuf, isShowCommentIdx, isInLine)) {
		luaL_error(L, "dump too deep or alloc memory failed!");
	}
	lua_pushlstring(L, (const char *)(pbuf->ptr), rbuf_len(pbuf));
	lua_pushinteger(L, rbuf_len(pbuf));
	rbuf_free(pbuf);
	return 2;
}
Пример #2
0
// convert every array to hash
static int SerializeHashDataIndent(lua_State *L)
{
	rbuf_t buf;
	rbuf_t *pbuf = &buf;
	rbuf_init(pbuf);
	ConvHashData(L, 0, pbuf);
	lua_pushlstring(L, (const char *)(pbuf->ptr), rbuf_len(pbuf));
	lua_pushinteger(L, rbuf_len(pbuf));
	rbuf_free(pbuf);
	return 2;
}
Пример #3
0
static void test()
{
	int i;
	const char *repr = "repr begin hello\t\tworld\n \b repr end";
	rbuf_t buf;
	rbuf_init(&buf);
	for (i=0; i<1000; i++) {
		rbuf_catprintf(&buf, "%d\n", i);
	}
	rbuf_catprintf(&buf, "hello\t\tworld %s\n", "kkk");
	rbuf_catrepr(&buf, repr, strlen(repr));
	printf("%s\n", buf.ptr);
	rbuf_free(&buf);
}
Пример #4
0
void worker_stop(worker_t* self) {
  int n, op = -1;
  void* ret;

  while (write(self->conn_queue[1], (char *) &op,
    sizeof(op)) != sizeof(op));

  pthread_join(self->thread, &ret);

  for (n = 0; n < num_workers; n++) {
    if (n != self->id)
      rbuf_free(self->outbox[n]);
  }

  free(self->outbox);
  free(self);
}
Пример #5
0
int main(void)
{
    rbuf b;

    // test rounding.
    {
        assert(rbuf_init(&b, 0, BYTE) == -1);
        rbuf_destroy(&b);

        assert(rbuf_init(&b, page_size/2, BYTE) == page_size);
        rbuf_destroy(&b);

        assert(rbuf_init(&b, page_size/2 + 1, BYTE) == page_size);
        rbuf_destroy(&b);

        assert(rbuf_init(&b, page_size, BYTE) == page_size);
        rbuf_destroy(&b);

        assert(rbuf_init(&b, page_size * 2, BYTE) == page_size * 2);
        rbuf_destroy(&b);

        assert(rbuf_init(&b, page_size * 3, BYTE) == page_size * 3);
        rbuf_destroy(&b);

        assert(rbuf_init(&b, (page_size * 3) - (page_size / 3), BYTE) == 
                page_size * 3);
        rbuf_destroy(&b);
    }

    // Test BYTE
    {
        rbuf_init(&b, page_size, BYTE);

        // fill buffer.
        for (int i = 0; i < b.size; i++) {
            rbuf_write(&b, "f", 1);
        }
        assert(rbuf_free(&b) == 0);

        // overfill buffer.
        assert(rbuf_write(&b, "f", 1) == 0);
        assert(rbuf_free(&b) == 0);

        // empty buffer.
        char c;
        for (int i = 0; i < b.size; i++) {
            rbuf_read(&b, &c, 1);
            assert(c == 'f');
        }
        assert(rbuf_count(&b) == 0);

        // read from empty buffer.
        assert(rbuf_read(&b, &c, 1) == 0);
        assert(rbuf_count(&b) == 0);

        rbuf_destroy(&b);
    }

    // Test STRING.
    {
        rbuf_init(&b, page_size, STRING);

        static char data[] = "test string";

        // fill buffer.
        while(rbuf_write(&b, data, strlen(data))); 
        assert(rbuf_free(&b) == 0);

        // overfill buffer.
        assert(rbuf_write(&b, data, strlen(data)) == 0);
        assert(rbuf_free(&b) == 0);

        // empty buffer
        char out[strlen(data) + 1];
        size_t read, old_read;
        while((read = rbuf_read(&b, out, strlen(data) + 1))) {
            old_read = read;
            assert(strcmp(out, data) <= 0);
        }

        assert(rbuf_count(&b) == 0);
        assert(out[old_read - 1] == '\0');
        assert((strlen(out) + 1) == old_read);
        assert((page_size % (strlen(data) + 1) == old_read));

        // read from empty buffer.
        assert(rbuf_read(&b, out, strlen(data) + 1) == 0);

        rbuf_destroy(&b);
    }

    // Test STREAM
    {
        rbuf_init(&b, page_size, STREAM);

        static char data[] = "test stream\n";

        // fill buffer.
        while(rbuf_write(&b, data, strlen(data)));
        assert(rbuf_free(&b) == 0);

        // overfill buffer.
        assert(rbuf_write(&b, data, strlen(data)) == 0);
        assert(rbuf_free(&b) == 0);

        // empty buffer
        char out[strlen(data)];

        while(rbuf_read(&b, out, strlen(data)) != -1) {
            assert(strncmp(out, data, strlen(out)) == 0);
        }
        assert(rbuf_count(&b) == page_size % strlen(data));

        // read from empty buffer.
        assert(rbuf_read(&b, out, strlen(data)) == -1);

        // Terminate end stream in buffer.
        assert(rbuf_count(&b) > 0);
        assert(rbuf_write(&b, data + rbuf_count(&b), 
                    strlen(data) - (page_size % strlen(data))));

        assert(rbuf_read(&b, out, strlen(data)) != -1);
        assert(strncmp(out, data, strlen(out)) == 0);

        rbuf_destroy(&b);
    }

    // All tests passed succesfully!
    return 0;
}