/[eiffelstudio]/trunk/eweasel/tests/incr071/managed_pointer.e
ViewVC logotype

Contents of /trunk/eweasel/tests/incr071/managed_pointer.e

Parent Directory Parent Directory | Revision Log Revision Log


Revision 65297 - (show annotations)
Thu Nov 30 20:22:33 2006 UTC (13 years ago) by manus
File size: 25975 byte(s)
Moved from trunk/Src/eweasel to trunk/eweasel so that a simple checkout of the source code is not penalized by the lenghty process of checking out all the tests of eweasel.
1 indexing
2 description: "[
3 To easily manage allocation and release of allocated C memory, and
4 to perform insertion of basic elements. Byte order is by default
5 platform specific.
6 Although memory allocation routines do not accept a zero sized pointer
7 MANAGED_POINTER does by allocating in fact a 1 byte sized pointer for
8 this particular case.
9 ]"
10 date: "$Date$"
11 revision: "$Revision$"
12
13 class
14 MANAGED_POINTER
15
16 inherit
17 DISPOSABLE
18 redefine
19 is_equal, copy
20 end
21
22 PLATFORM
23 redefine
24 is_equal, copy
25 end
26
27 create
28 make, make_from_array, make_from_pointer, share_from_pointer
29
30 feature {NONE} -- Initialization
31
32 make (n: INTEGER) is
33 -- Allocate `item' with `n' bytes.
34 require
35 n_non_negative: n >= 0
36 do
37 item := item.memory_calloc (1, n.max (1))
38 if item = default_pointer then
39 (create {EXCEPTIONS}).raise ("No more memory")
40 end
41 count := n
42 ensure
43 item_set: item /= default_pointer
44 count_set: count = n
45 is_shared_set: not is_shared
46 end
47
48 make_from_array (data: ARRAY [NATURAL_8]) is
49 -- Allocate `item' with `data.count' bytes and copy
50 -- content of `data' into `item'.
51 require
52 data_not_void: data /= Void
53 do
54 count := data.count
55 item := item.memory_alloc (count.max (1))
56 if item = default_pointer then
57 (create {EXCEPTIONS}).raise ("No more memory")
58 end
59 put_array (data, 0)
60 ensure
61 item_set: item /= default_pointer
62 count_set: count = data.count
63 is_shared_set: not is_shared
64 end
65
66 make_from_pointer (a_ptr: POINTER; n: INTEGER) is
67 -- Copy `a_count' bytes from `a_ptr' into current.
68 require
69 a_ptr_not_null: a_ptr /= default_pointer
70 n_non_negative: n >= 0
71 do
72 item := item.memory_alloc (n.max (1))
73 if item = default_pointer then
74 (create {EXCEPTIONS}).raise ("No more memory")
75 end
76 item.memory_copy (a_ptr, n)
77 count := n
78 ensure
79 item_set: item /= default_pointer
80 count_set: count = n
81 is_shared_set: not is_shared
82 end
83
84 share_from_pointer (a_ptr: POINTER; n: INTEGER) is
85 -- Use directly `a_ptr' with count `n' to hold current data.
86 require
87 a_ptr_valid: a_ptr = default_pointer implies n = 0
88 n_non_negative: n >= 0
89 do
90 item := a_ptr
91 count := n
92 is_shared := True
93 ensure
94 item_set: item = a_ptr
95 count_set: count = n
96 is_shared_set: is_shared
97 end
98
99 feature -- Settings
100
101 set_from_pointer (a_ptr: POINTER; n: INTEGER) is
102 -- Use directly `a_ptr' with count `n' to hold current data.
103 require
104 is_shared: is_shared
105 a_ptr_not_null: a_ptr = default_pointer implies n = 0
106 n_non_negative: n >= 0
107 do
108 item := a_ptr
109 count := n
110 ensure
111 item_set: item = a_ptr
112 count_set: count = n
113 is_shared_unchanged: is_shared
114 end
115
116 feature -- Access
117
118 item: POINTER
119 -- Access to allocated memory.
120
121 count: INTEGER
122 -- Number of elements that Current can hold.
123
124 is_shared: BOOLEAN
125 -- Is `item' shared with another memory area?
126
127 feature -- Comparison
128
129 is_equal (other: like Current): BOOLEAN is
130 -- Is `other' attached to an object considered equal to current object?
131 do
132 Result := count = other.count and then item.memory_compare (other.item, count)
133 end
134
135 feature -- Duplication
136
137 copy (other: like Current) is
138 -- Update current object using fields of object attached
139 -- to `other', so as to yield equal objects. If `is_shared'
140 -- and current is not large enough to hold `other' create
141 -- a new pointer area and `is_shared' is set to `False'.
142 do
143 if count >= other.count then
144 -- No need to reallocate, it is safe to just copy.
145 item.memory_copy (other.item, other.count)
146 else
147 -- We need to reallocate memory here
148 if is_shared then
149 -- Before `item' was shared, so we simply allocate
150 -- a new memory area from `other' and reset
151 -- the `is_shared' flag.
152 is_shared := False
153 make_from_pointer (other.item, other.count)
154 else
155 -- Simply resize Current and copy data.
156 resize (other.count)
157 item.memory_copy (other.item, other.count)
158 end
159 end
160 ensure then
161 sharing_status_not_preserved:
162 (old is_shared and not is_shared) implies (other.count > old count)
163 end
164
165 feature -- Access: Platform specific
166
167 read_natural_8 (pos: INTEGER): NATURAL_8 is
168 -- Read NATURAL_8 at position `pos'.
169 require
170 pos_nonnegative: pos >= 0
171 valid_position: (pos + natural_8_bytes) <= count
172 do
173 ($Result).memory_copy (item + pos, natural_8_bytes)
174 end
175
176 read_natural_16 (pos: INTEGER): NATURAL_16 is
177 -- Read NATURAL_16 at position `pos'.
178 require
179 pos_nonnegative: pos >= 0
180 valid_position: (pos + natural_16_bytes) <= count
181 do
182 ($Result).memory_copy (item + pos, natural_16_bytes)
183 end
184
185 read_natural_32 (pos: INTEGER): NATURAL_32 is
186 -- Read NATURAL_32 at position `pos'.
187 require
188 pos_nonnegative: pos >= 0
189 valid_position: (pos + natural_32_bytes) <= count
190 do
191 ($Result).memory_copy (item + pos, natural_32_bytes)
192 end
193
194 read_natural_64 (pos: INTEGER): NATURAL_64 is
195 -- Read NATURAL_64 at position `pos'.
196 require
197 pos_nonnegative: pos >= 0
198 valid_position: (pos + natural_64_bytes) <= count
199 do
200 ($Result).memory_copy (item + pos, natural_64_bytes)
201 end
202
203 read_integer_8 (pos: INTEGER): INTEGER_8 is
204 -- Read INTEGER_8 at position `pos'.
205 require
206 pos_nonnegative: pos >= 0
207 valid_position: (pos + integer_8_bytes) <= count
208 do
209 Result := read_natural_8 (pos).as_integer_8
210 end
211
212 read_integer_16 (pos: INTEGER): INTEGER_16 is
213 -- Read INTEGER_16 at position `pos'.
214 require
215 pos_nonnegative: pos >= 0
216 valid_position: (pos + integer_16_bytes) <= count
217 do
218 Result := read_natural_16 (pos).as_integer_16
219 end
220
221 read_integer_32 (pos: INTEGER): INTEGER is
222 -- Read INTEGER at position `pos'.
223 require
224 pos_nonnegative: pos >= 0
225 valid_position: (pos + integer_32_bytes) <= count
226 do
227 Result := read_natural_32 (pos).as_integer_32
228 end
229
230 read_integer_64 (pos: INTEGER): INTEGER_64 is
231 -- Read INTEGER_64 at position `pos'.
232 require
233 pos_nonnegative: pos >= 0
234 valid_position: (pos + integer_64_bytes) <= count
235 do
236 Result := read_natural_64 (pos).as_integer_64
237 end
238
239 read_pointer (pos: INTEGER): POINTER is
240 -- Read POINTER at position `pos'.
241 require
242 pos_nonnegative: pos >= 0
243 valid_position: (pos + Pointer_bytes) <= count
244 do
245 ($Result).memory_copy (item + pos, Pointer_bytes)
246 end
247
248 read_boolean (pos: INTEGER): BOOLEAN is
249 -- Read BOOLEAN at position `pos'.
250 require
251 pos_nonnegative: pos >= 0
252 valid_position: (pos + Boolean_bytes) <= count
253 do
254 ($Result).memory_copy (item + pos, Boolean_bytes)
255 end
256
257 read_character (pos: INTEGER): CHARACTER is
258 -- Read CHARACTER at position `pos'.
259 require
260 pos_nonnegative: pos >= 0
261 valid_position: (pos + Character_bytes) <= count
262 do
263 ($Result).memory_copy (item + pos, Character_bytes)
264 end
265
266 read_real (pos: INTEGER): REAL is
267 -- Read REAL_32 at position `pos'.
268 obsolete "Use read_real_32 instead."
269 require
270 pos_nonnegative: pos >= 0
271 valid_position: (pos + Real_bytes) <= count
272 do
273 ($Result).memory_copy (item + pos, Real_bytes)
274 end
275
276 read_real_32 (pos: INTEGER): REAL is
277 -- Read REAL_32 at position `pos'.
278 require
279 pos_nonnegative: pos >= 0
280 valid_position: (pos + Real_bytes) <= count
281 do
282 ($Result).memory_copy (item + pos, Real_bytes)
283 end
284
285 read_double (pos: INTEGER): DOUBLE is
286 -- Read REAL_64 at position `pos'.
287 obsolete "Use read_real_64 instead."
288 require
289 pos_nonnegative: pos >= 0
290 valid_position: (pos + Double_bytes) <= count
291 do
292 ($Result).memory_copy (item + pos, Double_bytes)
293 end
294
295 read_real_64 (pos: INTEGER): DOUBLE is
296 -- Read REAL_64 at position `pos'.
297 require
298 pos_nonnegative: pos >= 0
299 valid_position: (pos + Double_bytes) <= count
300 do
301 ($Result).memory_copy (item + pos, Double_bytes)
302 end
303
304 read_array (pos, a_count: INTEGER): ARRAY [NATURAL_8] is
305 -- Read `count' bytes at position `pos'.
306 require
307 pos_nonnegative: pos >= 0
308 count_positive: a_count > 0
309 valid_position: (pos + a_count) <= count
310 local
311 i: INTEGER
312 do
313 from
314 create Result.make (1, a_count)
315 until
316 i >= a_count
317 loop
318 Result.put (read_natural_8 (pos + i), i + 1)
319 i := i + 1
320 end
321 ensure
322 read_array_not_void: Result /= Void
323 read_array_valid_count: Result.count = a_count
324 end
325
326 feature -- Element change: Platform specific
327
328 put_natural_8 (i: NATURAL_8; pos: INTEGER) is
329 -- Insert `i' at position `pos'.
330 require
331 pos_nonnegative: pos >= 0
332 valid_position: (pos + natural_8_bytes) <= count
333 do
334 (item + pos).memory_copy ($i, natural_8_bytes)
335 ensure
336 inserted: i = read_natural_8 (pos)
337 end
338
339 put_natural_16 (i: NATURAL_16; pos: INTEGER) is
340 -- Insert `i' at position `pos'.
341 require
342 pos_nonnegative: pos >= 0
343 valid_position: (pos + natural_16_bytes) <= count
344 do
345 (item + pos).memory_copy ($i, natural_16_bytes)
346 ensure
347 inserted: i = read_natural_16 (pos)
348 end
349
350 put_natural_32 (i: NATURAL_32; pos: INTEGER) is
351 -- Insert `i' at position `pos'.
352 require
353 pos_nonnegative: pos >= 0
354 valid_position: (pos + natural_32_bytes) <= count
355 do
356 (item + pos).memory_copy ($i, natural_32_bytes)
357 ensure
358 inserted: i = read_natural_32 (pos)
359 end
360
361 put_natural_64 (i: NATURAL_64; pos: INTEGER) is
362 -- Insert `i' at position `pos'.
363 require
364 pos_nonnegative: pos >= 0
365 valid_position: (pos + natural_64_bytes) <= count
366 do
367 (item + pos).memory_copy ($i, natural_64_bytes)
368 ensure
369 inserted: i = read_natural_64 (pos)
370 end
371
372 put_integer_8 (i: INTEGER_8; pos: INTEGER) is
373 -- Insert `i' at position `pos'.
374 require
375 pos_nonnegative: pos >= 0
376 valid_position: (pos + integer_8_bytes) <= count
377 do
378 put_natural_8 (i.as_natural_8, pos)
379 ensure
380 inserted: i = read_integer_8 (pos)
381 end
382
383 put_integer_16 (i: INTEGER_16; pos: INTEGER) is
384 -- Insert `i' at position `pos'.
385 require
386 pos_nonnegative: pos >= 0
387 valid_position: (pos + integer_16_bytes) <= count
388 do
389 put_natural_16 (i.as_natural_16, pos)
390 ensure
391 inserted: i = read_integer_16 (pos)
392 end
393
394 put_integer_32 (i: INTEGER; pos: INTEGER) is
395 -- Insert `i' at position `pos'.
396 require
397 pos_nonnegative: pos >= 0
398 valid_position: (pos + integer_32_bytes) <= count
399 do
400 put_natural_32 (i.as_natural_32, pos)
401 ensure
402 inserted: i = read_integer_32 (pos)
403 end
404
405 put_integer_64 (i: INTEGER_64; pos: INTEGER) is
406 -- Insert `i' at position `pos'.
407 require
408 pos_nonnegative: pos >= 0
409 valid_position: (pos + integer_64_bytes) <= count
410 do
411 put_natural_64 (i.as_natural_64, pos)
412 ensure
413 inserted: i = read_integer_64 (pos)
414 end
415
416 put_pointer (p: POINTER; pos: INTEGER) is
417 -- Insert `p' at position `pos'.
418 require
419 pos_nonnegative: pos >= 0
420 valid_position: (pos + Pointer_bytes) <= count
421 do
422 (item + pos).memory_copy ($p, Pointer_bytes)
423 ensure
424 inserted: p = read_pointer (pos)
425 end
426
427 put_boolean (b: BOOLEAN; pos: INTEGER) is
428 -- Insert `b' at position `pos'.
429 require
430 pos_nonnegative: pos >= 0
431 valid_position: (pos + Boolean_bytes) <= count
432 do
433 (item + pos).memory_copy ($b, Boolean_bytes)
434 ensure
435 inserted: b = read_boolean (pos)
436 end
437
438 put_character (c: CHARACTER; pos: INTEGER) is
439 -- Insert `' at position `pos'.
440 require
441 pos_nonnegative: pos >= 0
442 valid_position: (pos + Character_bytes) <= count
443 do
444 (item + pos).memory_copy ($c, Character_bytes)
445 ensure
446 inserted: c = read_character (pos)
447 end
448
449 put_real (r: REAL; pos: INTEGER) is
450 -- Insert `r' at position `pos'.
451 obsolete "Use put_real_32 instead."
452 require
453 pos_nonnegative: pos >= 0
454 valid_position: (pos + Real_bytes) <= count
455 do
456 (item + pos).memory_copy ($r, Real_bytes)
457 ensure
458 inserted: r = read_real_32 (pos)
459 end
460
461 put_real_32 (r: REAL; pos: INTEGER) is
462 -- Insert `r' at position `pos'.
463 require
464 pos_nonnegative: pos >= 0
465 valid_position: (pos + Real_bytes) <= count
466 do
467 (item + pos).memory_copy ($r, Real_bytes)
468 ensure
469 inserted: r = read_real_32 (pos)
470 end
471
472 put_double (d: DOUBLE; pos: INTEGER) is
473 -- Insert `d' at position `pos'.
474 obsolete "Use put_real_64 instead."
475 require
476 pos_nonnegative: pos >= 0
477 valid_position: (pos + Double_bytes) <= count
478 do
479 (item + pos).memory_copy ($d, Double_bytes)
480 ensure
481 inserted: d = read_real_64 (pos)
482 end
483
484 put_real_64 (d: DOUBLE; pos: INTEGER) is
485 -- Insert `d' at position `pos'.
486 require
487 pos_nonnegative: pos >= 0
488 valid_position: (pos + Double_bytes) <= count
489 do
490 (item + pos).memory_copy ($d, Double_bytes)
491 ensure
492 inserted: d = read_real_64 (pos)
493 end
494
495 put_array (data: ARRAY [NATURAL_8]; pos: INTEGER) is
496 -- Copy content of `data' into `item' at position `pos'.
497 require
498 data_not_void: data /= Void
499 pos_nonnegative: pos >= 0
500 valid_position: (pos + data.count) <= count
501 local
502 l_sp: SPECIAL [NATURAL_8]
503 do
504 l_sp := data.area;
505 (item + pos).memory_copy ($l_sp, data.count)
506 ensure
507 inserted: data.is_equal (read_array (pos, data.count))
508 end
509
510 feature -- Access: Little-endian format
511
512 read_natural_8_le (pos: INTEGER): NATURAL_8 is
513 -- Read NATURAL_8 at position `pos'.
514 require
515 pos_nonnegative: pos >= 0
516 valid_position: (pos + natural_8_bytes) <= count
517 do
518 ($Result).memory_copy (item + pos, natural_8_bytes)
519 end
520
521 read_natural_16_le (pos: INTEGER): NATURAL_16 is
522 -- Read NATURAL_16 at position `pos'.
523 require
524 pos_nonnegative: pos >= 0
525 valid_position: (pos + natural_16_bytes) <= count
526 local
527 l_high, l_low: NATURAL_16
528 do
529 if is_little_endian then
530 Result := read_natural_16 (pos)
531 else
532 l_low := {NATURAL_16} 0x00FF & read_natural_8 (pos)
533 l_high := read_natural_8 (pos + natural_8_bytes)
534 Result := (l_high.to_natural_16 |<< 8) | l_low
535 end
536 end
537
538 read_natural_32_le (pos: INTEGER): NATURAL_32 is
539 -- Read NATURAL_32 at position `pos'.
540 require
541 pos_nonnegative: pos >= 0
542 valid_position: (pos + natural_32_bytes) <= count
543 local
544 l_high, l_low: NATURAL_32
545 do
546 if is_little_endian then
547 Result := read_natural_32 (pos)
548 else
549 l_low := {NATURAL_32} 0x0000FFFF & read_natural_16_le (pos)
550 l_high := read_natural_16_le (pos + natural_16_bytes)
551 Result := (l_high.to_natural_32 |<< 16) | l_low
552 end
553 end
554
555 read_natural_64_le (pos: INTEGER): NATURAL_64 is
556 -- Read NATURAL_64 at position `pos'.
557 require
558 pos_nonnegative: pos >= 0
559 valid_position: (pos + natural_64_bytes) <= count
560 local
561 l_high, l_low: NATURAL_64
562 do
563 if is_little_endian then
564 Result := read_natural_64 (pos)
565 else
566 l_low := {NATURAL_64} 0x00000000FFFFFFFF & read_natural_32_le (pos)
567 l_high := read_natural_32_le (pos + natural_32_bytes)
568 Result := (l_high.to_natural_64 |<< 32) | l_low
569 end
570 end
571
572 read_integer_8_le (pos: INTEGER): INTEGER_8 is
573 -- Read INTEGER_8 at position `pos'.
574 require
575 pos_nonnegative: pos >= 0
576 valid_position: (pos + integer_8_bytes) <= count
577 do
578 Result := read_natural_8_le (pos).as_integer_8
579 end
580
581 read_integer_16_le (pos: INTEGER): INTEGER_16 is
582 -- Read INTEGER_16 at position `pos'.
583 require
584 pos_nonnegative: pos >= 0
585 valid_position: (pos + integer_16_bytes) <= count
586 do
587 Result := read_natural_16_le (pos).as_integer_16
588 end
589
590 read_integer_32_le (pos: INTEGER): INTEGER is
591 -- Read INTEGER at position `pos'.
592 require
593 pos_nonnegative: pos >= 0
594 valid_position: (pos + integer_32_bytes) <= count
595 do
596 Result := read_natural_32_le (pos).as_integer_32
597 end
598
599 read_integer_64_le (pos: INTEGER): INTEGER_64 is
600 -- Read INTEGER_64 at position `pos'.
601 require
602 pos_nonnegative: pos >= 0
603 valid_position: (pos + integer_64_bytes) <= count
604 do
605 Result := read_natural_64_le (pos).as_integer_64
606 end
607
608 feature -- Element change: Little-endian format
609
610 put_natural_8_le (i: NATURAL_8; pos: INTEGER) is
611 -- Insert `i' at position `pos' in big-endian format.
612 require
613 pos_nonnegative: pos >= 0
614 valid_position: (pos + natural_8_bytes) <= count
615 do
616 (item + pos).memory_copy ($i, natural_8_bytes)
617 ensure
618 inserted: i = read_natural_8_le (pos)
619 end
620
621 put_natural_16_le (i: NATURAL_16; pos: INTEGER) is
622 -- Insert `i' at position `pos' in big-endian format.
623 require
624 pos_nonnegative: pos >= 0
625 valid_position: (pos + natural_16_bytes) <= count
626 do
627 if is_little_endian then
628 put_natural_16 (i, pos)
629 else
630 put_natural_8 ((i & 0x00FF).to_natural_8, pos)
631 put_natural_8 ((((i & 0xFF00) |>> 8) & 0x00FF).to_natural_8, pos + natural_8_bytes)
632 end
633 ensure
634 inserted: i = read_natural_16_le (pos)
635 end
636
637 put_natural_32_le (i: NATURAL_32; pos: INTEGER) is
638 -- Insert `i' at position `pos' in big-endian format.
639 require
640 pos_nonnegative: pos >= 0
641 valid_position: (pos + natural_32_bytes) <= count
642 do
643 if is_little_endian then
644 put_natural_32 (i, pos)
645 else
646 put_natural_16_le ((i & 0x0000FFFF).to_natural_16, pos)
647 put_natural_16_le ((((i & 0xFFFF0000) |>> 16) & 0x0000FFFF).to_natural_16, pos + natural_16_bytes)
648 end
649 ensure
650 inserted: i = read_natural_32_le (pos)
651 end
652
653 put_natural_64_le (i: NATURAL_64; pos: INTEGER) is
654 -- Insert `i' at position `pos' in big-endian format.
655 require
656 pos_nonnegative: pos >= 0
657 valid_position: (pos + natural_64_bytes) <= count
658 do
659 if is_little_endian then
660 put_natural_64 (i, pos)
661 else
662 put_natural_32_le ((i & 0x00000000FFFFFFFF).to_natural_32, pos)
663 put_natural_32_le (
664 (((i & 0xFFFFFFFF00000000) |>> 32) & 0x00000000FFFFFFFF).to_natural_32,
665 pos+ natural_32_bytes)
666 end
667 ensure
668 inserted: i = read_natural_64_le (pos)
669 end
670
671 put_integer_8_le (i: INTEGER_8; pos: INTEGER) is
672 -- Insert `i' at position `pos' in big-endian format.
673 require
674 pos_nonnegative: pos >= 0
675 valid_position: (pos + integer_8_bytes) <= count
676 do
677 put_natural_8_le (i.as_natural_8, pos)
678 ensure
679 inserted: i = read_integer_8_le (pos)
680 end
681
682 put_integer_16_le (i: INTEGER_16; pos: INTEGER) is
683 -- Insert `i' at position `pos' in big-endian format.
684 require
685 pos_nonnegative: pos >= 0
686 valid_position: (pos + integer_16_bytes) <= count
687 do
688 put_natural_16_le (i.as_natural_16, pos)
689 ensure
690 inserted: i = read_integer_16_le (pos)
691 end
692
693 put_integer_32_le (i: INTEGER; pos: INTEGER) is
694 -- Insert `i' at position `pos' in big-endian format.
695 require
696 pos_nonnegative: pos >= 0
697 valid_position: (pos + integer_32_bytes) <= count
698 do
699 put_natural_32_le (i.as_natural_32, pos)
700 ensure
701 inserted: i = read_integer_32_le (pos)
702 end
703
704 put_integer_64_le (i: INTEGER_64; pos: INTEGER) is
705 -- Insert `i' at position `pos' in big-endian format.
706 require
707 pos_nonnegative: pos >= 0
708 valid_position: (pos + integer_64_bytes) <= count
709 do
710 put_natural_64_le (i.as_natural_64, pos)
711 ensure
712 inserted: i = read_integer_64_le (pos)
713 end
714
715 feature -- Access: Big-endian format
716
717 read_natural_8_be (pos: INTEGER): NATURAL_8 is
718 -- Read NATURAL_8 at position `pos'.
719 require
720 pos_nonnegative: pos >= 0
721 valid_position: (pos + natural_8_bytes) <= count
722 do
723 ($Result).memory_copy (item + pos, natural_8_bytes)
724 end
725
726 read_natural_16_be (pos: INTEGER): NATURAL_16 is
727 -- Read NATURAL_16 at position `pos'.
728 require
729 pos_nonnegative: pos >= 0
730 valid_position: (pos + natural_16_bytes) <= count
731 local
732 l_high, l_low: NATURAL_16
733 do
734 if is_little_endian then
735 l_high := read_natural_8 (pos)
736 l_low := (0x00FF).to_natural_16 & read_natural_8 (pos + natural_8_bytes)
737 Result := (l_high.to_natural_16 |<< 8) | l_low
738 else
739 Result := read_natural_16 (pos)
740 end
741 end
742
743 read_natural_32_be (pos: INTEGER): NATURAL_32 is
744 -- Read NATURAL_32 at position `pos'.
745 require
746 pos_nonnegative: pos >= 0
747 valid_position: (pos + natural_32_bytes) <= count
748 local
749 l_high, l_low: NATURAL_32
750 do
751 if is_little_endian then
752 l_high := read_natural_16_be (pos)
753 l_low := (0x0000FFFF).to_natural_32 & read_natural_16_be (pos + natural_16_bytes)
754 Result := (l_high.to_natural_32 |<< 16) | l_low
755 else
756 Result := read_natural_32 (pos)
757 end
758 end
759
760 read_natural_64_be (pos: INTEGER): NATURAL_64 is
761 -- Read NATURAL_64 at position `pos'.
762 require
763 pos_nonnegative: pos >= 0
764 valid_position: (pos + natural_64_bytes) <= count
765 local
766 l_high, l_low: NATURAL_64
767 do
768 if is_little_endian then
769 l_high := read_natural_32_be (pos)
770 l_low := {NATURAL_64} 0x00000000FFFFFFFF & read_natural_32_be (pos + natural_32_bytes)
771 Result := (l_high.to_natural_64 |<< 32) | l_low
772 else
773 Result := read_natural_64 (pos)
774 end
775 end
776
777 read_integer_8_be (pos: INTEGER): INTEGER_8 is
778 -- Read INTEGER_8 at position `pos'.
779 require
780 pos_nonnegative: pos >= 0
781 valid_position: (pos + integer_8_bytes) <= count
782 do
783 Result := read_natural_8_be (pos).as_integer_8
784 end
785
786 read_integer_16_be (pos: INTEGER): INTEGER_16 is
787 -- Read INTEGER_16 at position `pos'.
788 require
789 pos_nonnegative: pos >= 0
790 valid_position: (pos + integer_16_bytes) <= count
791 do
792 Result := read_natural_16_be (pos).as_integer_16
793 end
794
795 read_integer_32_be (pos: INTEGER): INTEGER is
796 -- Read INTEGER at position `pos'.
797 require
798 pos_nonnegative: pos >= 0
799 valid_position: (pos + integer_32_bytes) <= count
800 do
801 Result := read_natural_32_be (pos).as_integer_32
802 end
803
804 read_integer_64_be (pos: INTEGER): INTEGER_64 is
805 -- Read INTEGER_64 at position `pos'.
806 require
807 pos_nonnegative: pos >= 0
808 valid_position: (pos + integer_64_bytes) <= count
809 do
810 Result := read_natural_64_be (pos).as_integer_64
811 end
812
813 feature -- Element change: Big-endian format
814
815 put_natural_8_be (i: NATURAL_8; pos: INTEGER) is
816 -- Insert `i' at position `pos' in big-endian format.
817 require
818 pos_nonnegative: pos >= 0
819 valid_position: (pos + natural_8_bytes) <= count
820 do
821 (item + pos).memory_copy ($i, natural_8_bytes)
822 ensure
823 inserted: i = read_natural_8_be (pos)
824 end
825
826 put_natural_16_be (i: NATURAL_16; pos: INTEGER) is
827 -- Insert `i' at position `pos' in big-endian format.
828 require
829 pos_nonnegative: pos >= 0
830 valid_position: (pos + natural_16_bytes) <= count
831 do
832 if is_little_endian then
833 put_natural_8 ((((i & 0xFF00) |>> 8) & 0x00FF).to_natural_8, pos)
834 put_natural_8 ((i & 0x00FF).to_natural_8, pos + natural_8_bytes)
835 else
836 put_natural_16 (i, pos)
837 end
838 ensure
839 inserted: i = read_natural_16_be (pos)
840 end
841
842 put_natural_32_be (i: NATURAL_32; pos: INTEGER) is
843 -- Insert `i' at position `pos' in big-endian format.
844 require
845 pos_nonnegative: pos >= 0
846 valid_position: (pos + natural_32_bytes) <= count
847 do
848 if is_little_endian then
849 put_natural_16_be ((((i & 0xFFFF0000) |>> 16) & 0x0000FFFF).to_natural_16, pos)
850 put_natural_16_be ((i & 0x0000FFFF).to_natural_16, pos + natural_16_bytes)
851 else
852 put_natural_32 (i, pos)
853 end
854 ensure
855 inserted: i = read_natural_32_be (pos)
856 end
857
858 put_natural_64_be (i: NATURAL_64; pos: INTEGER) is
859 -- Insert `i' at position `pos' in big-endian format.
860 require
861 pos_nonnegative: pos >= 0
862 valid_position: (pos + natural_64_bytes) <= count
863 do
864 if is_little_endian then
865 put_natural_32_be (
866 (((i & 0xFFFFFFFF00000000) |>> 32) & 0x00000000FFFFFFFF).to_natural_32, pos)
867 put_natural_32_be ((i & 0x00000000FFFFFFFF).to_natural_32,
868 pos + natural_32_bytes)
869 else
870 put_natural_64 (i, pos)
871 end
872 ensure
873 inserted: i = read_natural_64_be (pos)
874 end
875
876 put_integer_8_be (i: INTEGER_8; pos: INTEGER) is
877 -- Insert `i' at position `pos' in big-endian format.
878 require
879 pos_nonnegative: pos >= 0
880 valid_position: (pos + integer_8_bytes) <= count
881 do
882 put_natural_8_be (i.as_natural_8, pos)
883 ensure
884 inserted: i = read_integer_8_be (pos)
885 end
886
887 put_integer_16_be (i: INTEGER_16; pos: INTEGER) is
888 -- Insert `i' at position `pos' in big-endian format.
889 require
890 pos_nonnegative: pos >= 0
891 valid_position: (pos + integer_16_bytes) <= count
892 do
893 put_natural_16_be (i.as_natural_16, pos)
894 ensure
895 inserted: i = read_integer_16_be (pos)
896 end
897
898 put_integer_32_be (i: INTEGER; pos: INTEGER) is
899 -- Insert `i' at position `pos' in big-endian format.
900 require
901 pos_nonnegative: pos >= 0
902 valid_position: (pos + integer_32_bytes) <= count
903 do
904 put_natural_32_be (i.as_natural_32, pos)
905 ensure
906 inserted: i = read_integer_32_be (pos)
907 end
908
909 put_integer_64_be (i: INTEGER_64; pos: INTEGER) is
910 -- Insert `i' at position `pos' in big-endian format.
911 require
912 pos_nonnegative: pos >= 0
913 valid_position: (pos + integer_64_bytes) <= count
914 do
915 put_natural_64_be (i.as_natural_64, pos)
916 ensure
917 inserted: i = read_integer_64_be (pos)
918 end
919
920 feature -- Concatenation
921
922 append (other: like Current) is
923 -- Append `other' at the end of Current.
924 require
925 not_shared: not is_shared
926 other_not_void: other /= Void
927 local
928 new_count: INTEGER
929 do
930 new_count := count + other.count
931 item := item.memory_realloc (new_count.max (1))
932 if item = default_pointer then
933 (create {EXCEPTIONS}).raise ("No more memory")
934 end
935 (item + count).memory_copy (other.item, other.count)
936 count := new_count
937 end
938
939 feature -- Resizing
940
941 resize (n: INTEGER) is
942 -- Reallocate `item' to hold `n' bytes.
943 -- If `n' smaller than `count', does nothing.
944 require
945 not_shared: not is_shared
946 do
947 if n > count then
948 -- Reallocate.
949 check n_positive: n > 1 end
950 item := item.memory_realloc (n)
951 if item = default_pointer then
952 (create {EXCEPTIONS}).raise ("No more memory")
953 end
954
955 -- Reset newly allocated memory to `0'.
956 (item + count).memory_set (0, n - count)
957 count := n
958 end
959 end
960
961 feature {NONE} -- Disposal
962
963 dispose is
964 -- Release memory pointed by `item'.
965 local
966 null: POINTER
967 do
968 if not is_shared then
969 item.memory_free
970 end
971 item := null
972 is_shared := False
973 ensure then
974 shared_reset: not is_shared
975 end
976
977 invariant
978 item_not_null: item = default_pointer implies (count = 0 and is_shared)
979 valid_count: count >= 0
980
981 end -- class MANAGED_POINTER

Properties

Name Value
svn:eol-style native
svn:keywords Author Date Id Revision

  ViewVC Help
Powered by ViewVC 1.1.23