allocate 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 40ffef2ab0f089aa82c7f6f8a07a82124e45acb94832585e2bcab6a889aa61c5
4
+ data.tar.gz: a2580d56f8dedb4305dd28b057583ac3f4c4bda2a0bae8ee791915a14b7d1648
5
+ SHA512:
6
+ metadata.gz: c4e6295e9464dc0b633948c89113af3d25b88b00a4713e4f3cbb2be98bfd727daa5787bb779aa2617d1bdbd354224ce511208fd40157a49fae56d0d13d58a046
7
+ data.tar.gz: 1000a13809564528dab3fe05ab0edb0c8535a38f768728d692fe307a663704f7ca156004b65eb70f160a3aaed829a55ccb85b49fc598652224f95dec215ef1fe
@@ -0,0 +1,189 @@
1
+ // Author: Marek K.
2
+
3
+ /*
4
+
5
+ This program is free software: you can redistribute it and/or modify
6
+ it under the terms of the GNU General Public License as published by
7
+ the Free Software Foundation, either version 3 of the License, or
8
+ (at your option) any later version.
9
+
10
+ This program is distributed in the hope that it will be useful,
11
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
+ GNU General Public License for more details.
14
+
15
+ You should have received a copy of the GNU General Public License
16
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
17
+
18
+ Dieses Programm ist Freie Software: Sie koennen es unter den Bedingungen
19
+ der GNU General Public License, wie von der Free Software Foundation,
20
+ Version 3 der Lizenz oder (nach Ihrer Wahl) jeder neueren
21
+ veroeffentlichten Version, weiter verteilen und/oder modifizieren.
22
+
23
+ Dieses Programm wird in der Hoffnung bereitgestellt, dass es nuetzlich sein wird, jedoch
24
+ OHNE JEDE GEWAEHR,; sogar ohne die implizite
25
+ Gewaehr der MARKTFAEHIGKEIT oder EIGNUNG FUER EINEN BESTIMMTEN ZWECK.
26
+ Siehe die GNU General Public License fuer weitere Einzelheiten.
27
+
28
+ Sie sollten eine Kopie der GNU General Public License zusammen mit diesem
29
+ Programm erhalten haben. Wenn nicht, siehe <http://www.gnu.org/licenses/>.
30
+
31
+ */
32
+
33
+ #include <ruby.h>
34
+ #include <stdlib.h>
35
+ #include <stddef.h>
36
+
37
+ struct allocate_s
38
+ {
39
+ size_t size;
40
+ void * ptr;
41
+ };
42
+
43
+ static VALUE allocate_allocallocate(VALUE); /* Allocates the object */
44
+ static void allocate_allocatefree(void *); /* Releases the previously allocated memory of the object. */
45
+
46
+ static VALUE allocate_init(VALUE); /* Initializes the object */
47
+ static VALUE allocate_free(VALUE); /* Free memory */
48
+ static VALUE allocate_alloc(VALUE, VALUE); /* Allocated memory */
49
+ static VALUE allocate_good(VALUE); /* Check if the memory is ready */
50
+ static VALUE allocate_set(VALUE, VALUE, VALUE); /* Sets a value */
51
+ static VALUE allocate_get(VALUE, VALUE); /* Get a value */
52
+ static VALUE allocate_max(VALUE self);
53
+
54
+ void Init_allocate();
55
+
56
+ static void allocate_allocatefree(void * p)
57
+ {
58
+ struct allocate_s * ptr = (struct allocate_s *) p;
59
+
60
+ if(ptr->ptr != NULL)
61
+ free(ptr->ptr);
62
+ }
63
+
64
+ static VALUE allocate_allocallocate(VALUE klass)
65
+ {
66
+ VALUE obj;
67
+ struct allocate_s * ptr;
68
+
69
+ obj = Data_Make_Struct(klass, struct allocate_s, NULL, allocate_allocatefree, ptr);
70
+
71
+ ptr->size = 0;
72
+ ptr->ptr = NULL;
73
+
74
+ return obj;
75
+ }
76
+
77
+ static VALUE allocate_init(VALUE self)
78
+ {
79
+ //struct allocate_s * ptr;
80
+
81
+ //Data_Get_Struct(self, struct allocate_s, ptr);
82
+
83
+ return self;
84
+ }
85
+
86
+ static VALUE allocate_free(VALUE self)
87
+ {
88
+ struct allocate_s * ptr;
89
+
90
+ Data_Get_Struct(self, struct allocate_s, ptr);
91
+
92
+ if (0 == ptr->size)
93
+ return self;
94
+
95
+ free(ptr->ptr);
96
+
97
+ ptr->size = 0;
98
+ ptr->ptr = NULL;
99
+
100
+ return self;
101
+ }
102
+
103
+ static VALUE allocate_alloc(VALUE self, VALUE size)
104
+ {
105
+ struct allocate_s * ptr;
106
+
107
+ Data_Get_Struct(self, struct allocate_s, ptr);
108
+
109
+ if(NUM2UINT(size) == 0)
110
+ rb_raise(rb_eArgError, "At least 1 byte must be allocated.");
111
+
112
+ ptr->size = NUM2SIZET(size);
113
+ ptr->ptr = malloc(NUM2SIZET(size) * sizeof(unsigned char));
114
+
115
+ if(ptr->ptr == NULL)
116
+ rb_raise(rb_eArgError, "There was an error while allocating.");
117
+
118
+ return self;
119
+ }
120
+
121
+ static VALUE allocate_good(VALUE self)
122
+ {
123
+ struct allocate_s * ptr;
124
+
125
+ Data_Get_Struct(self, struct allocate_s, ptr);
126
+
127
+ return (ptr->size>0&&ptr->ptr!=NULL?Qtrue:Qfalse);
128
+ }
129
+
130
+ static VALUE allocate_set(VALUE self, VALUE index, VALUE size)
131
+ {
132
+ struct allocate_s * ptr;
133
+
134
+ Data_Get_Struct(self, struct allocate_s, ptr);
135
+
136
+ if(ptr->size > 0 && ptr->ptr != NULL)
137
+ {
138
+ ((unsigned char *) ptr->ptr)[NUM2SIZET(index)] = (unsigned char) NUM2CHR(size);
139
+ }
140
+ else
141
+ {
142
+ rb_raise(rb_eArgError, "An area that has not been allocated can not contain a value.");
143
+ }
144
+
145
+ return self;
146
+ }
147
+
148
+ static VALUE allocate_get(VALUE self, VALUE index)
149
+ {
150
+ struct allocate_s * ptr;
151
+
152
+ Data_Get_Struct(self, struct allocate_s, ptr);
153
+
154
+ if(ptr->size > 0 && ptr->ptr != NULL)
155
+ {
156
+ return UINT2NUM((unsigned int) ((unsigned char *) ptr->ptr)[NUM2SIZET(index)]);
157
+ }
158
+ else
159
+ {
160
+ rb_raise(rb_eArgError, "Can not read from an area that has not been allocated.");
161
+ }
162
+
163
+ return self;
164
+ }
165
+
166
+ static VALUE allocate_max(VALUE self)
167
+ {
168
+ struct allocate_s * ptr;
169
+
170
+ Data_Get_Struct(self, struct allocate_s, ptr);
171
+
172
+ return UINT2NUM(ptr->size);
173
+ }
174
+
175
+ void Init_allocate()
176
+ {
177
+ VALUE cAllocate;
178
+
179
+ cAllocate = rb_const_get(rb_cObject, rb_intern("Allocate"));
180
+
181
+ rb_define_alloc_func(cAllocate, allocate_allocallocate);
182
+ rb_define_method(cAllocate, "initialize", allocate_init, 0);
183
+ rb_define_method(cAllocate, "alloc", allocate_alloc, 1);
184
+ rb_define_method(cAllocate, "good?", allocate_good, 0);
185
+ rb_define_method(cAllocate, "set", allocate_set, 2);
186
+ rb_define_method(cAllocate, "get", allocate_get, 1);
187
+ rb_define_method(cAllocate, "max?", allocate_max, 0);
188
+ rb_define_method(cAllocate, "free", allocate_free, 0);
189
+ }
@@ -0,0 +1,7 @@
1
+ require "mkmf"
2
+
3
+ abort "missing malloc()" unless have_func "malloc"
4
+ abort "missing free()" unless have_func "free"
5
+
6
+ create_header
7
+ create_makefile "allocate/allocate"
data/lib/allocate.rb ADDED
@@ -0,0 +1,5 @@
1
+ class Allocate
2
+ VERSION = "1.0"
3
+ end
4
+
5
+ require "allocate/allocate"
@@ -0,0 +1,61 @@
1
+ # Author: Marek K.
2
+
3
+ =begin
4
+
5
+ This program is free software: you can redistribute it and/or modify
6
+ it under the terms of the GNU General Public License as published by
7
+ the Free Software Foundation, either version 3 of the License, or
8
+ (at your option) any later version.
9
+
10
+ This program is distributed in the hope that it will be useful,
11
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
+ GNU General Public License for more details.
14
+
15
+ You should have received a copy of the GNU General Public License
16
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
17
+
18
+ Dieses Programm ist Freie Software: Sie können es unter den Bedingungen
19
+ der GNU General Public License, wie von der Free Software Foundation,
20
+ Version 3 der Lizenz oder (nach Ihrer Wahl) jeder neueren
21
+ veröffentlichten Version, weiter verteilen und/oder modifizieren.
22
+
23
+ Dieses Programm wird in der Hoffnung bereitgestellt, dass es nützlich sein wird, jedoch
24
+ OHNE JEDE GEWÄHR,; sogar ohne die implizite
25
+ Gewähr der MARKTFÄHIGKEIT oder EIGNUNG FÜR EINEN BESTIMMTEN ZWECK.
26
+ Siehe die GNU General Public License für weitere Einzelheiten.
27
+
28
+ Sie sollten eine Kopie der GNU General Public License zusammen mit diesem
29
+ Programm erhalten haben. Wenn nicht, siehe <http://www.gnu.org/licenses/>.
30
+
31
+ =end
32
+
33
+ require "test/unit"
34
+ require "allocate"
35
+
36
+ class AllocateTest < Test::Unit::TestCase
37
+
38
+ def setup
39
+ @a = Allocate.new
40
+ end
41
+
42
+ def test_test
43
+ @a.alloc 4
44
+
45
+ assert_equal @a.good?, true
46
+
47
+ @a.set 0, 3
48
+ @a.set 1, 6
49
+ @a.set 2, 9
50
+ @a.set 3, 12
51
+
52
+ assert_equal @a.get(0), 3
53
+ assert_equal @a.get(1), 6
54
+ assert_equal @a.get(2), 9
55
+ assert_equal @a.get(3), 12
56
+ assert_equal @a.max?, 4
57
+
58
+ @a.free
59
+ assert_equal @a.good?, false
60
+ end
61
+ end
metadata ADDED
@@ -0,0 +1,52 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: allocate
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Marek K.
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-12-10 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Allocate requests memory by means of the C function malloc and can release
14
+ this memory with the C function free again. It is also possible to set values in
15
+ the memory area.
16
+ email: m.k@mk16.de
17
+ executables: []
18
+ extensions:
19
+ - ext/allocate/extconf.rb
20
+ extra_rdoc_files: []
21
+ files:
22
+ - ext/allocate/allocate.c
23
+ - ext/allocate/extconf.rb
24
+ - lib/allocate.rb
25
+ - test/test_allocate.rb
26
+ homepage: http://test.mk16.de/projects/allocate-gem
27
+ licenses:
28
+ - GPL-3.0
29
+ metadata:
30
+ documentation_uri: http://test.mk16.de/projects/allocate-gem
31
+ source_code_uri: http://test.mk16.de/scriptFiles/allocate/
32
+ post_install_message:
33
+ rdoc_options: []
34
+ require_paths:
35
+ - lib
36
+ required_ruby_version: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ required_rubygems_version: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ requirements: []
47
+ rubyforge_project:
48
+ rubygems_version: 2.7.8
49
+ signing_key:
50
+ specification_version: 4
51
+ summary: Interface between Ruby and the C functions malloc and free.
52
+ test_files: []