diff options
author | Jeremy Kerr <jk@ozlabs.org> | 2015-05-19 17:05:38 +0800 |
---|---|---|
committer | Stewart Smith <stewart@linux.vnet.ibm.com> | 2015-05-21 15:03:34 +1000 |
commit | a88495a1439ff20345870a82565658249be55d23 (patch) | |
tree | a5eda82d74c525f4e9fa502eea97f774075f1bba /core | |
parent | 8ab7a78c23c5e62566646aa9d5ac8749dfd73837 (diff) | |
download | skiboot-a88495a1439ff20345870a82565658249be55d23.zip skiboot-a88495a1439ff20345870a82565658249be55d23.tar.gz skiboot-a88495a1439ff20345870a82565658249be55d23.tar.bz2 |
core: Add mem_region_next
This change adds a function to iterate mem_regions.
Signed-off-by: Jeremy Kerr <jk@ozlabs.org>
Signed-off-by: Stewart Smith <stewart@linux.vnet.ibm.com>
Diffstat (limited to 'core')
-rw-r--r-- | core/mem_region.c | 14 | ||||
-rw-r--r-- | core/test/Makefile.check | 14 | ||||
-rw-r--r-- | core/test/run-mem_region_next.c | 117 |
3 files changed, 144 insertions, 1 deletions
diff --git a/core/mem_region.c b/core/mem_region.c index 0a1538d..f6d5a7a 100644 --- a/core/mem_region.c +++ b/core/mem_region.c @@ -1008,3 +1008,17 @@ void mem_region_add_dt_reserved(void) free(names); free(ranges); } + +struct mem_region *mem_region_next(struct mem_region *region) +{ + struct list_node *node; + + assert(lock_held_by_me(&mem_region_lock)); + + node = region ? ®ion->list : ®ions.n; + + if (node->next == ®ions.n) + return NULL; + + return list_entry(node->next, struct mem_region, list); +} diff --git a/core/test/Makefile.check b/core/test/Makefile.check index 3044ecc..b1555e2 100644 --- a/core/test/Makefile.check +++ b/core/test/Makefile.check @@ -1,5 +1,17 @@ # -*-Makefile-*- -CORE_TEST := core/test/run-device core/test/run-mem_region core/test/run-malloc core/test/run-malloc-speed core/test/run-mem_region_init core/test/run-mem_region_release_unused core/test/run-mem_region_release_unused_noalloc core/test/run-mem_region_reservations core/test/run-trace core/test/run-msg core/test/run-pel core/test/run-pool core/test/run-timer +CORE_TEST := core/test/run-device \ + core/test/run-mem_region \ + core/test/run-malloc \ + core/test/run-malloc-speed \ + core/test/run-mem_region_init \ + core/test/run-mem_region_next \ + core/test/run-mem_region_release_unused \ + core/test/run-mem_region_release_unused_noalloc \ + core/test/run-mem_region_reservations \ + core/test/run-trace core/test/run-msg \ + core/test/run-pel \ + core/test/run-pool \ + core/test/run-timer CORE_TEST_NOSTUB := core/test/run-console-log CORE_TEST_NOSTUB += core/test/run-console-log-buf-overrun diff --git a/core/test/run-mem_region_next.c b/core/test/run-mem_region_next.c new file mode 100644 index 0000000..209ce67 --- /dev/null +++ b/core/test/run-mem_region_next.c @@ -0,0 +1,117 @@ +/* Copyright 2013-2015 IBM Corp. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + * implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include <config.h> + +#define BITS_PER_LONG (sizeof(long) * 8) +/* Don't include this, it's PPC-specific */ +#define __CPU_H +static unsigned int cpu_max_pir = 1; +struct cpu_thread { + unsigned int chip_id; +}; + +#include <stdlib.h> +#include <string.h> + +/* Use these before we override definitions below. */ +static void *real_malloc(size_t size) +{ + return malloc(size); +} + +static void real_free(void *p) +{ + return free(p); +} + +#undef malloc +#undef free + +#include <skiboot.h> + +#define is_rodata(p) true + +#include "../mem_region.c" +#include "../malloc.c" +#include "../device.c" + +#include <assert.h> +#include <stdio.h> + +void lock(struct lock *l) +{ + assert(!l->lock_val); + l->lock_val++; +} + +void unlock(struct lock *l) +{ + assert(l->lock_val); + l->lock_val--; +} + +bool lock_held_by_me(struct lock *l) +{ + return l->lock_val; +} + + +#define TEST_HEAP_ORDER 12 +#define TEST_HEAP_SIZE (1ULL << TEST_HEAP_ORDER) + +int main(void) +{ + struct mem_region *r; + char *test_heap; + + /* Use malloc for the heap, so valgrind can find issues. */ + test_heap = real_malloc(TEST_HEAP_SIZE); + skiboot_heap.start = (unsigned long)test_heap; + skiboot_heap.len = TEST_HEAP_SIZE; + + lock(&mem_region_lock); + + /* empty regions */ + r = mem_region_next(NULL); + assert(!r); + + r = new_region("test.1", 0x1000, 0x1000, NULL, REGION_RESERVED); + assert(add_region(r)); + r = new_region("test.2", 0x2000, 0x1000, NULL, REGION_RESERVED); + assert(add_region(r)); + mem_regions_finalised = true; + + r = mem_region_next(NULL); + assert(r); + assert(r->start = 0x1000); + assert(r->len = 0x1000); + assert(r->type = REGION_RESERVED); + + r = mem_region_next(r); + assert(r); + assert(r->start = 0x2000); + assert(r->len = 0x1000); + assert(r->type = REGION_RESERVED); + + r = mem_region_next(r); + assert(!r); + + unlock(&mem_region_lock); + real_free(test_heap); + + return 0; +} |