@@ -658,7 +658,7 @@ PHP_APCU_API zend_bool apc_sma_get_avail_size(apc_sma_t* sma, size_t size) {
658
658
return 0 ;
659
659
}
660
660
661
- PHP_APCU_API zend_bool apc_sma_defrag (apc_sma_t * sma , size_t size ) {
661
+ PHP_APCU_API zend_bool apc_sma_defrag (apc_sma_t * sma , apc_sma_relocate_f relocate ) {
662
662
int32_t i ;
663
663
size_t realsize = ALIGNWORD (size + ALIGNWORD (sizeof (struct block_t )));
664
664
@@ -677,10 +677,56 @@ PHP_APCU_API zend_bool apc_sma_defrag(apc_sma_t* sma, size_t size) {
677
677
while (cur -> fnext ) {
678
678
cur = BLOCKAT (cur -> fnext );
679
679
680
- if (cur -> size >= realsize ) {
681
- SMA_UNLOCK (sma , i );
682
- return 1 ;
680
+ /* Abort if last block is reached */
681
+ if (cur -> fnext )
682
+ break ;
683
+
684
+ /* Try to move an allocated block to cur */
685
+ block_t * nxt = NEXT_SBLOCK (cur );
686
+ while (OFFSET (nxt ) < cur -> fnext ) {
687
+ block_t backup_cur = * cur ;
688
+ block_t backup_nxt = * nxt ;
689
+
690
+ /* relocate block (skip if it can't be relocated) */
691
+ if (!relocate ((char * ) nxt + ALIGNWORD (sizeof (block_t )), (char * ) cur + ALIGNWORD (sizeof (block_t ))))
692
+ continue ;
693
+
694
+ cur -> size = backup_nxt .size ;
695
+ nxt = NEXT_SBLOCK (cur );
696
+ cur -> fnext = OFFSET (nxt );
697
+ SET_CANARY (cur );
698
+ #if 0
699
+ cur -> id = backup_nxt .id ;
700
+ #endif
701
+
702
+ /* */
703
+ nxt -> fnext = backup_cur .fnext ;
704
+ nxt -> fprev = backup_cur .fprev ;
705
+ BLOCKAT (nxt -> fnext )-> fprev = OFFSET (nxt );
706
+ BLOCKAT (nxt -> fprev )-> fnext = OFFSET (nxt );
707
+
708
+
709
+
710
+ nxt -> prev_size = 0 ; /* block is alloc'd */
711
+ nxt -> size = backup_cur .size ; /* and fix the size */
712
+ NEXT_SBLOCK (nxt )-> prev_size = nxt -> size ; /* adjust size */
713
+ SET_CANARY (nxt );
714
+
715
+ /* replace cur with next in free list */
716
+ nxt -> fnext = cur -> fnext ;
717
+ nxt -> fprev = cur -> fprev ;
718
+ BLOCKAT (nxt -> fnext )-> fprev = OFFSET (nxt );
719
+ BLOCKAT (nxt -> fprev )-> fnext = OFFSET (nxt );
720
+
721
+
722
+
723
+ sma_deallocate (void * shmaddr , size_t offset )
724
+
725
+
726
+ used = NEXT_SBLOCK (used );
683
727
}
728
+
729
+
684
730
}
685
731
686
732
SMA_UNLOCK (sma , i );
0 commit comments