30 #include <sys/types.h>
44 #include "debug_lock.h"
45 #include "shm_arena.h"
80 height_t left_ht, right_ht;
84 left_ht = seg ? seg->
height : -1;
87 right_ht = seg ? seg->
height : -1;
89 p_hdr->
height = (left_ht > right_ht) ? left_ht+1 : right_ht+1;
94 mapnum_t x_mapnum, offset_t x_offset,
95 mapnum_t *top_mapnum, offset_t *top_offset)
114 y = get_seg_header(a, y_mapnum, y_offset);
127 *top_mapnum = y_mapnum;
128 *top_offset = y_offset;
134 mapnum_t x_mapnum, offset_t x_offset,
135 mapnum_t *top_mapnum, offset_t *top_offset)
154 y = get_seg_header(a, y_mapnum, y_offset);
167 *top_mapnum = y_mapnum;
168 *top_offset = y_offset;
174 mapnum_t x_mapnum, offset_t x_offset,
175 mapnum_t *top_mapnum, offset_t *top_offset)
178 int right_difference;
185 right_difference = difference(a, y);
187 if(right_difference == 0)
209 rotate_left(a, x, x_mapnum, x_offset, top_mapnum, top_offset);
214 if(right_difference < 0)
235 rotate_left(a, x, x_mapnum, x_offset, top_mapnum, top_offset);
289 rotate_left(a, x, x_mapnum, x_offset, top_mapnum, top_offset);
298 mapnum_t x_mapnum, offset_t x_offset,
299 mapnum_t *top_mapnum, offset_t *top_offset)
311 left_difference = difference(a, y);
313 if(left_difference == 0)
317 rotate_right(a, x, x_mapnum, x_offset, top_mapnum, top_offset);
322 if(left_difference > 0)
325 rotate_right(a, x, x_mapnum, x_offset, top_mapnum, top_offset);
342 rotate_right(a, x, x_mapnum, x_offset, top_mapnum, top_offset);
356 int _shm_compare_free(
const struct seg_header *seg1,
357 mapnum_t mapnum1, offset_t top1,
359 mapnum_t mapnum0, offset_t top0)
369 if(mapnum1 > mapnum0)
371 if(mapnum1 < mapnum0)
387 int _shm_compare_allocated(
const struct seg_header *seg1,
388 mapnum_t mapnum1 __attribute__ ((unused)),
389 offset_t top1 __attribute__ ((unused)),
391 mapnum_t mapnum0 __attribute__ ((unused)),
392 offset_t top0 __attribute__ ((unused)))
396 cmp = strcmp(get_seg_name(seg1), get_seg_name(seg0));
398 return (cmp>0) ? 1 : ((cmp) ? -1 : 0);
408 mapnum_t *parent_mapnum,
409 offset_t *parent_offset,
410 mapnum_t *return_mapnum
420 *return_mapnum = seg_mapnum;
450 cmp = difference(a, seg);
454 balance_left(a, seg, *parent_mapnum, *parent_offset,
455 parent_mapnum, parent_offset);
458 balance_right(a, seg, *parent_mapnum, *parent_offset,
459 parent_mapnum, parent_offset);
467 detach_from_parent(
const struct shm_arena *a,
469 mapnum_t *parent_mapnum,
470 offset_t *parent_offset)
481 mapnum_t left_most_mapnum;
497 *parent_mapnum = left_most_mapnum;
498 *parent_offset = get_offset(a, left_most_seg, left_most_mapnum);
508 if(left_most_seg != r_seg)
511 fix_height(a, r_seg);
512 cmp = difference(a, r_seg);
516 balance_left(a, r_seg,
521 balance_right(a, r_seg,
528 fix_height(a, l_seg);
529 cmp = difference(a, l_seg);
533 balance_left(a, r_seg,
538 balance_right(a, r_seg,
544 fix_height(a, left_most_seg);
545 cmp = difference(a, left_most_seg);
549 balance_left(a, left_most_seg,
550 *parent_mapnum, *parent_offset,
551 parent_mapnum, parent_offset);
554 balance_right(a, left_most_seg,
555 *parent_mapnum, *parent_offset,
556 parent_mapnum, parent_offset);
598 int detach_node(
const struct shm_arena *a,
601 mapnum_t *parent_mapnum,
602 offset_t *parent_offset,
603 compare_func_t compare_func)
613 parent = get_seg_header(a, *parent_mapnum, *parent_offset);
614 cmp = compare_func(seg, seg_mapnum, get_offset(a, seg, seg_mapnum),
615 parent, *parent_mapnum, *parent_offset);
624 ret = detach_node(a, seg, seg_mapnum,
632 detach_from_parent(a, seg,
645 SPEW(_INFO,
"Segment node was not found");
656 ret = detach_node(a, seg, seg_mapnum,
664 detach_from_parent(a, seg,
677 SPEW(_INFO,
"Segment node was not found");
684 ASSERT(seg == parent);
688 fix_height(a, parent);
690 cmp = difference(a, parent);
694 balance_left(a, parent, *parent_mapnum, *parent_offset,
695 parent_mapnum, parent_offset);
698 balance_right(a, parent, *parent_mapnum, *parent_offset,
699 parent_mapnum, parent_offset);
708 void _shm_insert_node(
const struct shm_arena *a,
711 mapnum_t *parent_mapnum,
712 offset_t *parent_offset,
713 compare_func_t compare_func)
718 parent = get_seg_header(a, *parent_mapnum, *parent_offset);
719 cmp = compare_func(seg, seg_mapnum, get_offset(a, seg, seg_mapnum),
720 parent, *parent_mapnum, *parent_offset);
731 _shm_insert_node(a, seg, seg_mapnum,
750 _shm_insert_node(a, seg, seg_mapnum,
759 parent->
left_offset = get_offset(a, seg, seg_mapnum);
764 fix_height(a, parent);
766 cmp = difference(a, parent);
768 ASSERT(cmp <= 2 && cmp >= -2);
772 balance_left(a, parent, *parent_mapnum, *parent_offset,
773 parent_mapnum, parent_offset);
776 balance_right(a, parent, *parent_mapnum, *parent_offset,
777 parent_mapnum, parent_offset);
785 _shm_detach_free_segment(
const struct shm_arena *a,
799 if(a_hdr->
free_offset == get_offset(a, seg, mapnum) &&
813 cmp = difference(a, seg);
829 return (detach_node(a, seg, mapnum,
831 _shm_compare_free))?0:1;
836 _shm_detach_alloc_segment(
const struct shm_arena *a,
864 cmp = difference(a, seg);
878 return (detach_node(a, seg, mapnum,
880 _shm_compare_allocated))?0:1;
896 mapnum_t mapnum, offset_t offset)
904 ASSERT(mapnum >= 0 && mapnum < a->num_mappings);
911 seg = get_seg_header(a, mapnum, offset);
913 p = get_seg_header(a, p_mapnum = a_hdr->
free_mapnum,
918 cmp = _shm_compare_free(seg, mapnum, offset, p, p_mapnum, p_offset);
struct arena_header * header
struct shm_mapping * mapping