summaryrefslogtreecommitdiffstats
path: root/firmware/test/buflib/test_move2.c
blob: 1076c27a502a73afc5fbce3995ad33ebbe4f3c78 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
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 <stdio.h>
#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 (<unallocated>)
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;
}