Contiki 2.5
ram-segments.c
1 #ifndef __RAM_SEGMENTS_C__1POIF5E8U4__
2 #define __RAM_SEGMENTS_C__1POIF5E8U4__
3 
4 #include <loader/elfloader-otf.h>
5 #include <loader/codeprop-otf.h>
6 #include <sys/types.h>
7 #include <string.h>
8 #include <stdio.h>
9 
10 struct ram_output
11 {
12  struct elfloader_output output;
13  char *base;
14  unsigned int offset;
15  void *text;
16  void *rodata;
17  void *data;
18  void *bss;
19 };
20 
21 static void *
22 allocate_segment(struct elfloader_output * const output,
23  unsigned int type, int size)
24 {
25  struct ram_output * const ram = (struct ram_output *)output;
26  void *block = malloc(size);
27  if (!block) return NULL;
28  switch(type) {
29  case ELFLOADER_SEG_TEXT:
30  if (ram->text) free(ram->text);
31  ram->text = block;
32  break;
33  case ELFLOADER_SEG_RODATA:
34  if (ram->rodata) free(ram->rodata);
35  ram->rodata = block;
36  break;
37  case ELFLOADER_SEG_DATA:
38  if (ram->data) free(ram->data);
39  ram->data = block;
40  break;
41  case ELFLOADER_SEG_BSS:
42  if (ram->bss) free(ram->bss);
43  ram->bss = block;
44  break;
45  default:
46  free(block);
47  return NULL;
48  }
49  return block;
50 }
51 
52 static int
53 start_segment(struct elfloader_output *output,
54  unsigned int type, void *addr, int size)
55 {
56  ((struct ram_output*)output)->base = addr;
57  ((struct ram_output*)output)->offset = 0;
58  return ELFLOADER_OK;
59 }
60 
61 static int
62 end_segment(struct elfloader_output *output)
63 {
64  return ELFLOADER_OK;
65 }
66 
67 static int
68 write_segment(struct elfloader_output *output, const char *buf,
69  unsigned int len)
70 {
71  struct ram_output * const ram = (struct ram_output *)output;
72  memcpy(ram->base + ram->offset, buf, len);
73  ram->offset += len;
74  return len;
75 }
76 
77 static unsigned int
78 segment_offset(struct elfloader_output *output)
79 {
80  return ((struct ram_output*)output)->offset;
81 }
82 
83 static const struct elfloader_output_ops elf_output_ops =
84  {
85  allocate_segment,
86  start_segment,
87  end_segment,
88  write_segment,
89  segment_offset
90  };
91 
92 
93 static struct ram_output seg_output = {
94  {&elf_output_ops},
95  NULL,
96  0,
97  NULL,
98  NULL,
99  NULL,
100  NULL
101 };
102 
103 PROCESS(ram_segments_cleanup_process, "RAM segments cleanup process");
104 
105 PROCESS_THREAD(ram_segments_cleanup_process, ev, data)
106 {
107  PROCESS_BEGIN();
108  while(1) {
109  PROCESS_WAIT_EVENT_UNTIL(ev == PROCESS_EVENT_EXITED
110  || ev == PROCESS_EVENT_EXIT);
111  if (ev == PROCESS_EVENT_EXIT) break;
113  elfloader_autostart_processes[0] == data) {
114  PROCESS_PAUSE(); /* Let the process exit */
115  if (seg_output.text) {
116  free(seg_output.text);
117  seg_output.text = NULL;
118  }
119  if (seg_output.rodata) {
120  free(seg_output.rodata);
121  seg_output.rodata = NULL;
122  }
123  if (seg_output.data) {
124  free(seg_output.data);
125  seg_output.data = NULL;
126  }
127 
128  if (seg_output.bss) {
129  free(seg_output.bss);
130  seg_output.bss = NULL;
131  }
133  }
134  }
135  PROCESS_END();
136 }
137 struct elfloader_output *codeprop_output = &seg_output.output;
138 
139 #endif /* __RAM_SEGMENTS_C__1POIF5E8U4__ */