1
0
Fork 0
forked from len0rd/rockbox

Bring abroad second buflib test

Interfaces with core_alloc_* instead of buflib directly.

Provide UT_core_allocator_init() with
a fixed buffer size for predictable results.

Change-Id: I26a7b3101f7782063547940bded52d8202638394
This commit is contained in:
Thomas Jarosch 2015-01-03 17:06:21 +01:00
parent e7d94323bc
commit 7d5f133007
4 changed files with 154 additions and 2 deletions

View file

@ -1,15 +1,20 @@
FIRMWARE=../..
CC ?= gcc
CFLAGS += -g -O2 -DDEBUG -D__PCTOOL__ -DBUFLIB_DEBUG_BLOCKS -std=gnu99 -I$(FIRMWARE)/include -I$(FIRMWARE)/export -I.
# Note: Don't be fooled by MEMORYSIZE here
# We have a fixed, predictable buffer in UT_core_allocator_init()
CFLAGS += -g -O2 -DDEBUG -D__PCTOOL__ -DBUFLIB_UNIT_TEST -DMEMORYSIZE=8 -DBUFLIB_DEBUG_BLOCKS -std=gnu99 -I$(FIRMWARE)/include -I$(FIRMWARE)/export -I.
LDFLAGS += -L. -lpthread
.PHONY: clean all
TARGETS_OBJ = test_main.o
TARGETS_OBJ = test_main.o \
test_main2.o
TARGETS = $(TARGETS_OBJ:.o=)
LIB_OBJ = buflib.o \
core_alloc.o \
crc32.o \
strlcpy.o \
util.o
@ -36,6 +41,9 @@ $(TARGETS): $(TARGETS_OBJ) $(LIB_FILE)
buflib.o: $(FIRMWARE)/buflib.c
$(CC) $(CFLAGS) -c $< -o $@
core_alloc.o: $(FIRMWARE)/core_alloc.c
$(CC) $(CFLAGS) -c $< -o $@
crc32.o: $(FIRMWARE)/common/crc32.c
$(CC) $(CFLAGS) -c $< -o $@

View file

@ -0,0 +1,108 @@
/***************************************************************************
* __________ __ ___.
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
* \/ \/ \/ \/ \/
* $Id$
*
* Copyright (C) 2011 Thomas Martitz
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
****************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include "core_alloc.h"
#include "util.h"
/*
* Expected output (64-bit):
-------------------
-------------------
*/
#define error(...) do { printf(__VA_ARGS__); exit(1); } while(0)
static int move_callback(int handle, void* old, void* new)
{
printf("MOVED!\n");
return BUFLIB_CB_OK;
}
static int shrink_callback(int handle, unsigned hints, void* start, size_t size)
{
char* buf = start;
size_t wanted = hints & BUFLIB_SHRINK_SIZE_MASK;
if (handle == 4)
{
buf+=1, size-=1;
memmove(buf, buf-1, (size < 20) ? size : 20);
core_shrink(handle, buf, size);
return BUFLIB_CB_OK;
}
return BUFLIB_CB_CANNOT_SHRINK;
}
struct buflib_callbacks ops = {
.move_callback = move_callback,
.shrink_callback = shrink_callback,
};
struct buflib_callbacks ops2 = {
.move_callback = NULL,
.shrink_callback = shrink_callback,
};
int main(void)
{
size_t size2, size4;
UT_core_allocator_init();
printf("available: %zu\n", core_available());
int first = core_alloc("first, fixed", 4<<10);
if (first <= 0) error("first failed\n");
printf("available: %zu\n", core_available());
int second = core_alloc_maximum("second, var", &size2, &ops);
if (second <= 0) error("second failed\n");
printf("second size: %zu\n", size2);
strcpy(core_get_data(second), "begin");
strcpy(core_get_data(second)+124, "end");
printf("%s\n", core_get_name(second));
if (!core_shrink(second, core_get_data(second), 128))
error("shrink second failed\n");
int third = core_alloc("third, fixed", 20<<10);
if (third <= 0) error("third failed");
strcpy(core_get_data(third), "third");
printf("available: %zu\n", core_available());
int fourth = core_alloc_maximum("fourth", &size4, &ops2);
if (fourth <= 0) error("fourth failed\n");
core_print_blocks(&print_simple);
if (!core_shrink(fourth, core_get_data(fourth)+(5<<10), size4-(5<<10)))
{
error("shrink fourth failed\n");
}
sprintf(core_get_data(fourth), "fourth size: %zu", size4);
core_print_blocks(&print_simple);
int fifth = core_alloc("fifth, fixed", 6<<10);
if (fifth <= 0) error("fifth failed\n");
printf("%s\n", core_get_data(fourth));
core_print_blocks(&print_simple);
core_print_allocs(&print_simple);
return 0;
}

View file

@ -20,9 +20,42 @@
****************************************************************************/
#include "util.h"
#include "stdio.h"
#include "buflib.h"
#include "system.h"
void print_simple(const char *str)
{
printf("%s\n", str);
}
void print_handle(int handle_num, const char *str)
{
(void)handle_num;
printf("%s\n", str);
}
/* fake core_allocator_init() with a fixed 50kb buffer size */
void UT_core_allocator_init()
{
extern struct buflib_context core_ctx;
static char buf[50<<10];
unsigned char *raw_start = buf;
unsigned char *aligned_start = ALIGN_UP(raw_start, sizeof(intptr_t));
buflib_init(&core_ctx, aligned_start, sizeof(buf) - (aligned_start - raw_start));
}
/* TODO: those should be part of core_alloc */
void core_print_blocks(void (*print)(const char*))
{
(void)print;
extern struct buflib_context core_ctx;
buflib_print_blocks(&core_ctx, &print_handle);
}
void core_print_allocs(void (*print)(const char*))
{
(void)print;
extern struct buflib_context core_ctx;
buflib_print_allocs(&core_ctx, &print_handle);
}

View file

@ -22,6 +22,9 @@
#ifndef _TEST_UTIL_H
#define _TEST_UTIL_H
void print_simple(const char *string);
void print_handle(int handle_num, const char *string);
void UT_core_allocator_init();
#endif