From 5f824e21e0cecf232dd5c6b48f4467fae31ec663 Mon Sep 17 00:00:00 2001 From: Thomas Jarosch Date: Sat, 3 Jan 2015 17:36:37 +0100 Subject: [PATCH] Add and adapt buflib move tests Change-Id: I57929f8f6a18cf9570f7358d48ad33f285b9ab0f --- firmware/test/buflib/Makefile | 4 +- firmware/test/buflib/test_move.c | 63 ++++++++++++++ firmware/test/buflib/test_move2.c | 132 ++++++++++++++++++++++++++++++ 3 files changed, 198 insertions(+), 1 deletion(-) create mode 100644 firmware/test/buflib/test_move.c create mode 100644 firmware/test/buflib/test_move2.c diff --git a/firmware/test/buflib/Makefile b/firmware/test/buflib/Makefile index 0f664dd143..9349c05206 100644 --- a/firmware/test/buflib/Makefile +++ b/firmware/test/buflib/Makefile @@ -9,7 +9,9 @@ LDFLAGS += -L. -lpthread .PHONY: clean all TARGETS_OBJ = test_main.o \ - test_main2.o + test_main2.o \ + test_move.o \ + test_move2.o TARGETS = $(TARGETS_OBJ:.o=) diff --git a/firmware/test/buflib/test_move.c b/firmware/test/buflib/test_move.c new file mode 100644 index 0000000000..cf6168a311 --- /dev/null +++ b/firmware/test/buflib/test_move.c @@ -0,0 +1,63 @@ +/*************************************************************************** +* __________ __ ___. +* 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 +#include "core_alloc.h" +#include "util.h" + +static int move_size; +int move_callback(int handle, void* old, void* new) +{ + move_size = (char*)old-(char*)new; + printf("Move! %s, %p, %p, %zu\n", core_get_name(handle), old, new, + move_size); + + return BUFLIB_CB_OK; +} + +struct buflib_callbacks ops = { + .move_callback = move_callback, + .shrink_callback = NULL, +}; + +int main(void) +{ + UT_core_allocator_init(); + + int first = core_alloc("first", 20<<10); + int second= core_alloc_ex("second", 20<<10, &ops); + strcpy(core_get_data(second), "Here's data"); + + core_free(first); + /* should not trigger compaction, but replace the just freed one */ + int third = core_alloc("third", 20<<10); + core_free(third); + /* should trigger compaction since it's a bit bigger than the just freed one */ + int fourth = core_alloc("fourth", 21<<10); + + int ret = !(!strcmp(core_get_data(second), "Here's data") && move_size >= 20<<10); + + core_print_blocks(&print_simple); + + core_free(second); + core_free(third); + + return ret; +} diff --git a/firmware/test/buflib/test_move2.c b/firmware/test/buflib/test_move2.c new file mode 100644 index 0000000000..1076c27a50 --- /dev/null +++ b/firmware/test/buflib/test_move2.c @@ -0,0 +1,132 @@ +/*************************************************************************** +* __________ __ ___. +* 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 +#include "core_alloc.h" +#include "util.h" +/* + * Expected output (64bit) +-------------------- +after freeing first: available: 10040 +after freeing forth: available: 10040 +buflib_compact(): Compacting! +move_block(): moving "third"(id=3) by -3210(-25680) +Move! third, 0x608758, 0x602308, 25680 +Cannot move now for handle 3 +fifth failed. Retrying... +buflib_compact(): Compacting! +move_block(): moving "third"(id=3) by -3210(-25680) +Move! third, 0x608758, 0x602308, 25680 +fifth handle: 1 +fifth(1): 0x608730 + 0x608758 + 21544 +second(2): 0x607308 + 0x607330 + 5160 +third(3): 0x6022e0 + 0x602308 + 15400 +sixth(4): 0x605f08 + 0x605f30 + 2088 +seventh(5): 0x606730 + 0x606758 + 552 +0x6022e0: val: 1925 (third) +0x605f08: val: 261 (sixth) +0x606730: val: 69 (seventh) +0x606958: val: -310 () +0x607308: val: 645 (second) +0x608730: val: 2693 (fifth) +-------------------- +*/ + +static int move_size, retry; +int move_callback(int handle, void* old, void* new) +{ + move_size = (char*)old-(char*)new; + printf("Move! %s, %p, %p, %d\n", core_get_name(handle), old, new, + move_size); + + if (!retry) + { + retry = 1; + printf("Cannot move now for handle %d\n", handle); + return BUFLIB_CB_CANNOT_MOVE; + } + return BUFLIB_CB_OK; +} + +struct buflib_callbacks ops = { + .move_callback = move_callback, + .shrink_callback = NULL, +}; + +struct buflib_callbacks ops_no_move = { + .move_callback = NULL, + .shrink_callback = NULL, +}; + +int main(void) +{ + UT_core_allocator_init(); + + int first = core_alloc("first", 20<<10); + int second= core_alloc_ex("second", 5<<10, &ops_no_move); + int third = core_alloc_ex("third", 15<<10, &ops); + strcpy(core_get_data(second), "Here's data"); + + core_free(first); + printf("after freeing first: available: %zu\n", core_available()); + /* should not trigger compaction, but replace the just freed one */ + int fourth = core_alloc("forth", 20<<10); + core_free(fourth); + printf("after freeing forth: available: %zu\n", core_available()); + /* should trigger compaction since it's a bit bigger than the just freed one */ + int fifth = core_alloc("fifth", 21<<10); + if (fifth <= 0) + { + printf("fifth failed. Retrying...\n"); + fifth = core_alloc("fifth", 21<<10); + } + if (fifth <= 0) + { + printf("fifth still failed\n"); + } + + printf("fifth handle: %d\n", fifth); + int sixth = core_alloc("sixth", 2<<10); + int seventh = core_alloc("seventh", 512); + + + core_print_allocs(&print_simple); + core_print_blocks(&print_simple); + int ret = !(!strcmp(core_get_data(second), "Here's data") && move_size >= 20<<10); + + core_free(second); + core_free(third); + if (fifth > 0) + core_free(fifth); + core_free(sixth); + core_free(seventh); + + return ret; +}