excel_to_code 0.0.8 → 0.0.9

Sign up to get free protection for your applications and to get access to all the features.
@@ -62,9 +62,13 @@ class ExcelToC < ExcelToX
62
62
  o.puts
63
63
  o.puts "// Used to decide whether to recalculate a cell"
64
64
  o.puts "static int variable_set[#{number_of_refs}];"
65
+ o.puts ""
66
+ o.puts "// Used to reset all cached values and free up memory"
67
+ # FIXME: This feels like a bad place for this. Should be in runtime?
65
68
  o.puts "void reset() {"
66
69
  o.puts " int i;"
67
70
  o.puts " cell_counter = 0;"
71
+ o.puts " free_all_allocated_memory(); "
68
72
  o.puts " for(i = 0; i < #{number_of_refs}; i++) {"
69
73
  o.puts " variable_set[i] = 0;"
70
74
  o.puts " }"
Binary file
@@ -11,6 +11,7 @@
11
11
  // Probably bad practice. At the very least, I should make it
12
12
  // link to the cell reference in some way.
13
13
  #define MAX_EXCEL_VALUE_HEAP_SIZE 1000000
14
+ #define MAX_MEMORY_TO_BE_FREED_HEAP_SIZE 100
14
15
 
15
16
  #define true 1
16
17
  #define false 0
@@ -74,11 +75,34 @@ static ExcelValue sumproduct(int number_of_arguments, ExcelValue *arguments);
74
75
  static ExcelValue vlookup_3(ExcelValue lookup_value_v,ExcelValue lookup_table_v, ExcelValue column_number_v);
75
76
  static ExcelValue vlookup(ExcelValue lookup_value_v,ExcelValue lookup_table_v, ExcelValue column_number_v, ExcelValue match_type_v);
76
77
 
77
- // My little heap
78
+ // My little heap for excel values
78
79
  ExcelValue cells[MAX_EXCEL_VALUE_HEAP_SIZE];
79
80
  int cell_counter = 0;
80
81
 
81
- #define HEAPCHECK if(cell_counter >= MAX_EXCEL_VALUE_HEAP_SIZE) { printf("Heap exceeded"); exit(-1); }
82
+ #define HEAPCHECK if(cell_counter >= MAX_EXCEL_VALUE_HEAP_SIZE) { printf("ExcelValue heap full. Edit MAX_EXCEL_VALUE_HEAP_SIZE in the c source code."); exit(-1); }
83
+
84
+ // My little heap for keeping pointers to memory that I need to reclaim
85
+ void *memory_that_needs_to_be_freed[MAX_MEMORY_TO_BE_FREED_HEAP_SIZE];
86
+ int memory_that_needs_to_be_freed_counter = 0;
87
+
88
+ #define MEMORY_THAT_NEEDS_TO_BE_FREED_HEAP_CHECK
89
+
90
+ static void free_later(void *pointer) {
91
+ memory_that_needs_to_be_freed[memory_that_needs_to_be_freed_counter] = pointer;
92
+ memory_that_needs_to_be_freed_counter++;
93
+ if(memory_that_needs_to_be_freed_counter >= MAX_MEMORY_TO_BE_FREED_HEAP_SIZE) {
94
+ printf("Memory that needs to be freed heap full. Edit MAX_MEMORY_TO_BE_FREED_HEAP_SIZE in the c source code");
95
+ exit(-1);
96
+ }
97
+ }
98
+
99
+ static void free_all_allocated_memory() {
100
+ int i;
101
+ for(i = 0; i < memory_that_needs_to_be_freed_counter; i++) {
102
+ free(memory_that_needs_to_be_freed[i]);
103
+ }
104
+ memory_that_needs_to_be_freed_counter = 0;
105
+ }
82
106
 
83
107
  // The object initializers
84
108
  static ExcelValue new_excel_number(double number) {
@@ -111,11 +135,12 @@ static ExcelValue new_excel_range(void *array, int rows, int columns) {
111
135
  };
112
136
 
113
137
  static void * new_excel_value_array(int size) {
114
- ExcelValue *pointer = malloc(sizeof(ExcelValue)*size);
138
+ ExcelValue *pointer = malloc(sizeof(ExcelValue)*size); // Freed later
115
139
  if(pointer == 0) {
116
140
  printf("Out of memory\n");
117
141
  exit(-1);
118
142
  }
143
+ free_later(pointer);
119
144
  return pointer;
120
145
  };
121
146
 
@@ -134,7 +159,6 @@ const ExcelValue EIGHT = {.type = ExcelNumber, .number = 8};
134
159
  const ExcelValue NINE = {.type = ExcelNumber, .number = 9};
135
160
  const ExcelValue TEN = {.type = ExcelNumber, .number = 10};
136
161
 
137
-
138
162
  // Booleans
139
163
  const ExcelValue TRUE = {.type = ExcelBoolean, .number = true };
140
164
  const ExcelValue FALSE = {.type = ExcelBoolean, .number = false };
@@ -688,17 +712,19 @@ static ExcelValue left(ExcelValue string_v, ExcelValue number_of_characters_v) {
688
712
  int number_of_characters = (int) number_from(number_of_characters_v);
689
713
  CHECK_FOR_CONVERSION_ERROR
690
714
 
691
- char *string;
715
+ char *string;
716
+ int string_must_be_freed = 0;
692
717
  switch (string_v.type) {
693
718
  case ExcelString:
694
719
  string = string_v.string;
695
720
  break;
696
721
  case ExcelNumber:
697
- string = malloc(20);
722
+ string = malloc(20); // Freed
698
723
  if(string == 0) {
699
724
  printf("Out of memory");
700
725
  exit(-1);
701
726
  }
727
+ string_must_be_freed = 1;
702
728
  snprintf(string,20,"%f",string_v.number);
703
729
  break;
704
730
  case ExcelBoolean:
@@ -714,13 +740,17 @@ static ExcelValue left(ExcelValue string_v, ExcelValue number_of_characters_v) {
714
740
  return string_v;
715
741
  }
716
742
 
717
- char *left_string = malloc(number_of_characters+1);
743
+ char *left_string = malloc(number_of_characters+1); // Freed
718
744
  if(left_string == 0) {
719
745
  printf("Out of memory");
720
746
  exit(-1);
721
- }
747
+ }
748
+ free_later(left_string);
722
749
  memcpy(left_string,string,number_of_characters);
723
750
  left_string[number_of_characters] = '\0';
751
+ if(string_must_be_freed == 1) {
752
+ free(string);
753
+ }
724
754
  return new_excel_string(left_string);
725
755
  }
726
756
 
@@ -1033,27 +1063,31 @@ static ExcelValue roundup(ExcelValue number_v, ExcelValue decimal_places_v) {
1033
1063
  static ExcelValue string_join(int number_of_arguments, ExcelValue *arguments) {
1034
1064
  int allocated_length = 100;
1035
1065
  int used_length = 0;
1036
- char *string = malloc(allocated_length);
1066
+ char *string = malloc(allocated_length); // Freed later
1037
1067
  if(string == 0) {
1038
1068
  printf("Out of memory");
1039
1069
  exit(-1);
1040
- }
1070
+ }
1071
+ free_later(string);
1041
1072
  char *current_string;
1042
1073
  int current_string_length;
1074
+ int must_free_current_string;
1043
1075
  ExcelValue current_v;
1044
1076
  int i;
1045
1077
  for(i=0;i<number_of_arguments;i++) {
1078
+ must_free_current_string = 0;
1046
1079
  current_v = (ExcelValue) arguments[i];
1047
1080
  switch (current_v.type) {
1048
1081
  case ExcelString:
1049
1082
  current_string = current_v.string;
1050
1083
  break;
1051
1084
  case ExcelNumber:
1052
- current_string = malloc(20);
1085
+ current_string = malloc(20); // Freed
1053
1086
  if(current_string == 0) {
1054
1087
  printf("Out of memory");
1055
1088
  exit(-1);
1056
- }
1089
+ }
1090
+ must_free_current_string = 1;
1057
1091
  snprintf(current_string,20,"%g",current_v.number);
1058
1092
  break;
1059
1093
  case ExcelBoolean:
@@ -1077,6 +1111,9 @@ static ExcelValue string_join(int number_of_arguments, ExcelValue *arguments) {
1077
1111
  string = realloc(string,allocated_length);
1078
1112
  }
1079
1113
  memcpy(string + used_length, current_string, current_string_length);
1114
+ if(must_free_current_string == 1) {
1115
+ free(current_string);
1116
+ }
1080
1117
  used_length = used_length + current_string_length;
1081
1118
  }
1082
1119
  string = realloc(string,used_length+1);
@@ -1152,7 +1189,7 @@ static ExcelValue sumifs(ExcelValue sum_range_v, int number_of_arguments, ExcelV
1152
1189
  }
1153
1190
 
1154
1191
  // Now go through and set up the criteria
1155
- ExcelComparison *criteria = malloc(sizeof(ExcelComparison)*number_of_criteria);
1192
+ ExcelComparison *criteria = malloc(sizeof(ExcelComparison)*number_of_criteria); // freed at end of function
1156
1193
  if(criteria == 0) {
1157
1194
  printf("Out of memory\n");
1158
1195
  exit(-1);
@@ -1328,6 +1365,8 @@ static ExcelValue sumifs(ExcelValue sum_range_v, int number_of_arguments, ExcelV
1328
1365
  }
1329
1366
  }
1330
1367
  }
1368
+ // Tidy up
1369
+ free(criteria);
1331
1370
  return new_excel_number(total);
1332
1371
  }
1333
1372
 
@@ -1350,7 +1389,7 @@ static ExcelValue sumproduct(int number_of_arguments, ExcelValue *arguments) {
1350
1389
  int rows;
1351
1390
  int columns;
1352
1391
  ExcelValue current_value;
1353
- ExcelValue **ranges = malloc(sizeof(ExcelValue *)*number_of_arguments);
1392
+ ExcelValue **ranges = malloc(sizeof(ExcelValue *)*number_of_arguments); // Added free statements
1354
1393
  if(ranges == 0) {
1355
1394
  printf("Out of memory\n");
1356
1395
  exit(-1);
@@ -1375,9 +1414,11 @@ static ExcelValue sumproduct(int number_of_arguments, ExcelValue *arguments) {
1375
1414
  ranges[a] = current_value.array;
1376
1415
  break;
1377
1416
  case ExcelError:
1417
+ free(ranges);
1378
1418
  return current_value;
1379
1419
  break;
1380
1420
  case ExcelEmpty:
1421
+ free(ranges);
1381
1422
  return VALUE;
1382
1423
  break;
1383
1424
  default:
@@ -1402,6 +1443,7 @@ static ExcelValue sumproduct(int number_of_arguments, ExcelValue *arguments) {
1402
1443
  sum += product;
1403
1444
  }
1404
1445
  }
1446
+ free(ranges);
1405
1447
  return new_excel_number(sum);
1406
1448
  }
1407
1449
 
@@ -2027,6 +2069,9 @@ int test_functions() {
2027
2069
  ExcelValue sum_array_1[] = {sum_array_0_v};
2028
2070
  assert(sum(1,sum_array_1).number == 1253.8718091935484);
2029
2071
 
2072
+ // Release memory
2073
+ free_all_allocated_memory();
2074
+
2030
2075
  return 0;
2031
2076
  }
2032
2077
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: excel_to_code
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.8
4
+ version: 0.0.9
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-06-01 00:00:00.000000000 Z
12
+ date: 2012-06-08 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rubypeg
16
- requirement: &70200979061400 !ruby/object:Gem::Requirement
16
+ requirement: &70366938770280 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70200979061400
24
+ version_requirements: *70366938770280
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: nokogiri
27
- requirement: &70200979060900 !ruby/object:Gem::Requirement
27
+ requirement: &70366938769760 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: 1.5.0
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *70200979060900
35
+ version_requirements: *70366938769760
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rspec
38
- requirement: &70200979060400 !ruby/object:Gem::Requirement
38
+ requirement: &70366938769220 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: 2.7.0
44
44
  type: :runtime
45
45
  prerelease: false
46
- version_requirements: *70200979060400
46
+ version_requirements: *70366938769220
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: ffi
49
- requirement: &70200979050020 !ruby/object:Gem::Requirement
49
+ requirement: &70366938768740 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,7 +54,7 @@ dependencies:
54
54
  version: 1.0.11
55
55
  type: :runtime
56
56
  prerelease: false
57
- version_requirements: *70200979050020
57
+ version_requirements: *70366938768740
58
58
  description: ! "# excel_to_code\n\nConverts some excel spreadsheets (.xlsx, not .xls)
59
59
  into some other programming languages (currently ruby or c).\nThis allows the excel
60
60
  spreadsheets to be run programatically, without excel.\n\nIts cannonical source
@@ -84,11 +84,11 @@ files:
84
84
  - src/commands/excel_to_ruby.rb
85
85
  - src/commands/excel_to_x.rb
86
86
  - src/commands.rb
87
+ - src/compile/c/a.out
87
88
  - src/compile/c/compile_to_c.rb
88
89
  - src/compile/c/compile_to_c_header.rb
89
90
  - src/compile/c/compile_to_c_unit_test.rb
90
91
  - src/compile/c/excel_to_c_runtime.c
91
- - src/compile/c/excel_to_c_runtime.c.out
92
92
  - src/compile/c/map_formulae_to_c.rb
93
93
  - src/compile/c/map_sheet_names_to_c_names.rb
94
94
  - src/compile/c/map_values_to_c.rb