blob: f9763adf498f96bcc92b8de8d27e2b3194d7ce1c [file] [log] [blame]
David Reissea2cba82009-03-30 21:35:00 +00001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
David Reiss3000b5b2008-03-31 21:38:29 +000020#define _GNU_SOURCE
21#include <stdlib.h>
22#include <stdio.h>
23#include <time.h>
24#include <dlfcn.h>
25
26int copies;
27int non_copies;
28
29void *realloc(void *ptr, size_t size) {
30 static void *(*real_realloc)(void*, size_t) = NULL;
31 if (real_realloc == NULL) {
32 real_realloc = (void* (*) (void*, size_t)) dlsym(RTLD_NEXT, "realloc");
33 }
34
35 void *ret_ptr = (*real_realloc)(ptr, size);
36
37 if (ret_ptr == ptr) {
38 non_copies++;
39 } else {
40 copies++;
41 }
42
43 return ret_ptr;
44}
45
46
47struct TMemoryBuffer {
48 void* ptr;
49 int size;
50};
51
52int main(int argc, char *argv[]) {
53 int num_buffers;
54 int init_size;
55 int max_size;
56 int doublings;
57 int iterations;
58
59 if (argc < 6 ||
60 argc > 7 ||
61 (num_buffers = atoi(argv[1])) == 0 ||
62 (init_size = atoi(argv[2])) == 0 ||
63 (max_size = atoi(argv[3])) == 0 ||
64 init_size > max_size ||
65 (iterations = atoi(argv[4])) == 0 ||
66 (doublings = atoi(argv[5])) == 0 ||
67 (argc == 7 && atoi(argv[6]) == 0)) {
68 fprintf(stderr, "usage: realloc_test <num_buffers> <init_size> <max_size> <doublings> <iterations> [seed]\n");
69 exit(EXIT_FAILURE);
70 }
71
72 for ( int i = 0 ; i < argc ; i++ ) {
73 printf("%s ", argv[i]);
74 }
75 printf("\n");
76
77 if (argc == 7) {
78 srand(atoi(argv[6]));
79 } else {
80 srand(time(NULL));
81 }
82
83 struct TMemoryBuffer* buffers = calloc(num_buffers, sizeof(*buffers));
84 if (buffers == NULL) abort();
85
86 for ( int i = 0 ; i < num_buffers ; i++ ) {
87 buffers[i].size = max_size;
88 }
89
90 while (iterations --> 0) {
91 for ( int i = 0 ; i < doublings * num_buffers ; i++ ) {
92 struct TMemoryBuffer* buf = &buffers[rand() % num_buffers];
93 buf->size *= 2;
94 if (buf->size <= max_size) {
95 buf->ptr = realloc(buf->ptr, buf->size);
96 } else {
97 free(buf->ptr);
98 buf->size = init_size;
99 buf->ptr = malloc(buf->size);
100 }
101 if (buf->ptr == NULL) abort();
102 }
103 }
104
105 printf("Non-copied %d/%d (%.2f%%)\n", non_copies, copies + non_copies, 100.0 * non_copies / (copies + non_copies));
106 return 0;
107}