SpiNNFrontEndCommon  7.4.2
Common support code for user-facing front end systems.
malloc_extras.c
1 /*
2  * Copyright (c) 2019 The University of Manchester
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * https://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <sark.h>
18 #include <common-typedefs.h>
19 #include <debug.h>
20 #include <malloc_extras.h>
21 
23 #define MINUS_POINT 60
25 #define BYTE_TO_WORD 4
27 #define BUFFER_WORDS (MINUS_POINT / BYTE_TO_WORD)
29 #define MIN_SIZE_HEAP 32
30 
31 //============================================================================
32 // control flags
33 
35 bool to_print = false;
36 
39 bool use_dtcm = true;
40 
41 //============================================================================
42 
43 // ===========================================================================
44 // functions
45 
47  to_print = true;
48 }
49 
51  to_print = false;
52 }
53 
54 #if 0
55 static inline void terminate(uint result_code) __attribute__((noreturn));
56 #endif
57 void malloc_extras_terminate(uint result_code) {
58  vcpu_t *sark_virtual_processor_info = (vcpu_t *) SV_VCPU;
59  uint core = spin1_get_core_id();
60  sark_virtual_processor_info[core].user1 = result_code;
61 
62  if (result_code != EXITED_CLEANLY && result_code != EXIT_FAIL) {
63  rt_error(RTE_SWERR);
64  }
65  spin1_exit(0);
66 }
67 
68 void malloc_extras_free(void *ptr) {
69 
70  // if safe to free, free from the correct heap based off position.
71  if ((int) ptr >= DTCM_BASE && (int) ptr <= DTCM_TOP) {
72  if (to_print) {
73  log_info("freeing 0x%08x from DTCM", ptr);
74  }
75  sark_xfree(sark.heap, ptr, ALLOC_LOCK);
76  } else {
77  if (to_print) {
78  log_info("freeing 0x%08x from SDRAM", ptr);
79  }
80  sark_xfree(sv->sdram_heap, ptr, ALLOC_LOCK);
81  }
82 }
83 
84 void *malloc_extras_sdram_malloc(uint bytes) {
85 
86  // try SDRAM stolen from the cores synaptic matrix areas.
87  void *p = sark_xalloc(sv->sdram_heap, bytes, 0, ALLOC_LOCK);
88 
89  if (p == NULL) {
90  log_error("Failed to malloc %u bytes.\n", bytes);
91  }
92  if (to_print) {
93  log_info("Allocated %u bytes from SDRAM at 0x%08x", bytes, p);
94  }
95 
96  return p;
97 }
98 
99 void *malloc_extras_malloc(uint bytes) {
100 
101  // try DTCM if allowed (not safe if overused, due to stack overflows)
102  void *p = NULL;
103  if (use_dtcm) {
104  p = sark_alloc(bytes, 1);
105 
106  // if DTCM failed to malloc, go to SDRAM.
107  if (p == NULL) {
108  p = malloc_extras_sdram_malloc(bytes);
109  } else if (to_print) {
110  log_info("Allocated %u bytes from DTCM at 0x%08x", bytes, p);
111  }
112  // only use SDRAM. (safer to avoid stack overflows)
113  } else {
114  p = malloc_extras_sdram_malloc(bytes);
115  }
116 
117  // if no safety, the point is the point used by the application code.
118  return p;
119 }
Data type definitions for SpiNNaker Neuron-modelling.
SpiNNaker debug header file.
void log_error(const char *message,...)
This function logs errors. Errors usually indicate a serious fault in the program,...
void log_info(const char *message,...)
This function logs informational messages. This is the lowest level of message normally printed.
Support for adding debugging information to dynamic allocation.
@ EXITED_CLEANLY
Everything is fine.
Definition: malloc_extras.h:29
@ EXIT_FAIL
We went wrong but we dont want to RTE.
Definition: malloc_extras.h:31
void malloc_extras_terminate(uint result_code)
Stops execution with a result code.
Definition: malloc_extras.c:57
void malloc_extras_free(void *ptr)
Frees a pointer without any marker for application code.
Definition: malloc_extras.c:68
void * malloc_extras_sdram_malloc(uint bytes)
Mallocs a number of bytes from SDRAM.
Definition: malloc_extras.c:84
void * malloc_extras_malloc(uint bytes)
Allows a search of the 2 heaps available. (DTCM, stolen SDRAM)
Definition: malloc_extras.c:99
void malloc_extras_turn_off_print(void)
Turn off printing.
Definition: malloc_extras.c:50
void malloc_extras_turn_on_print(void)
Turn on printing.
Definition: malloc_extras.c:46