ObjFW  Check-in [93755dd482]

Overview
Comment:OF(Big)DataArray: Fix capacity handling.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 93755dd4823cd412729dd48016c1044ec8e3fe3dc8bad2df6f0e86a4ab55d59b
User & Date: js on 2013-06-26 16:33:35
Other Links: manifest | tags
Context
2013-06-26
17:18
Revive the PSP port. check-in: 530decab01 user: js tags: trunk
16:33
OF(Big)DataArray: Fix capacity handling. check-in: 93755dd482 user: js tags: trunk
2013-06-25
20:31
Make backtraces work with ARM EABI. check-in: 6e8777fc03 user: js tags: trunk
Changes

Modified src/OFDataArray.m from [d222b0db89] to [b741186d9f].

96
97
98
99
100
101
102

103

104

105
106
107
108

109

110

111
112
113
114
115
116
117
118
119
120
121
122
123

124
125
126
127
128
129
130
131
132
133




134
135
136
137
138
139
140
+ (instancetype)dataArrayWithBase64EncodedString: (OFString*)string
{
	return [[[self alloc] initWithBase64EncodedString: string] autorelease];
}

- init
{

	return [self initWithItemSize: 1

			     capacity: 0];

}

- initWithItemSize: (size_t)itemSize
{

	return [self initWithItemSize: itemSize

			     capacity: 0];

}

- initWithCapacity: (size_t)capacity
{
	return [self initWithItemSize: 1
			     capacity: capacity];
}

- initWithItemSize: (size_t)itemSize
	  capacity: (size_t)capacity
{
	self = [super init];


	if (itemSize == 0) {
		[self release];
		@throw [OFInvalidArgumentException exception];
	}

	_items = [self allocMemoryWithSize: itemSize
				     count: capacity];

	_itemSize = itemSize;
	_capacity = capacity;





	return self;
}

- initWithContentsOfFile: (OFString*)path
{
	@try {







>
|
>
|
>




>
|
>
|
>













>
|
|
|
|

|
|

|
|
>
>
>
>







96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
+ (instancetype)dataArrayWithBase64EncodedString: (OFString*)string
{
	return [[[self alloc] initWithBase64EncodedString: string] autorelease];
}

- init
{
	self = [super init];

	_itemSize = 1;

	return self;
}

- initWithItemSize: (size_t)itemSize
{
	self = [super init];

	_itemSize = itemSize;

	return self;
}

- initWithCapacity: (size_t)capacity
{
	return [self initWithItemSize: 1
			     capacity: capacity];
}

- initWithItemSize: (size_t)itemSize
	  capacity: (size_t)capacity
{
	self = [super init];

	@try {
		if (itemSize == 0) {
			[self release];
			@throw [OFInvalidArgumentException exception];
		}

		_items = [self allocMemoryWithSize: itemSize
					     count: capacity];

		_itemSize = itemSize;
		_capacity = capacity;
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

- initWithContentsOfFile: (OFString*)path
{
	@try {
665
666
667
668
669
670
671






672






673
674

675


676









677
678


679

680









681
682
683
684
685
686
687
688
689



690
691

692
693
694
695

696

697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712



713
714

715
716
717
718

719

720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737



738
739

740
741
742
743

744

745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775

776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799

800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
		 count: _count];

	return data;
}
@end

@implementation OFBigDataArray






- initWithItemSize: (size_t)itemSize






	  capacity: (size_t)capacity
{

	size_t lastPageByte = [OFSystemInfo pageSize] - 1;












	capacity = (capacity * itemSize + lastPageByte) & ~lastPageByte;



	return [super initWithItemSize: itemSize

			      capacity: capacity];









}

- (void)addItem: (const void*)item
{
	size_t size, lastPageByte;

	if (SIZE_MAX - _count < 1 || _count + 1 > SIZE_MAX / _itemSize)
		@throw [OFOutOfRangeException exception];




	lastPageByte = [OFSystemInfo pageSize] - 1;
	size = ((_count + 1) * _itemSize + lastPageByte) & ~lastPageByte;


	if (size > _capacity) {
		_items = [self resizeMemory: _items
				       size: size];

		_capacity = size;

	}

	memcpy(_items + _count * _itemSize, item, _itemSize);

	_count++;
	_size = size;
}

- (void)addItems: (const void*)items
	   count: (size_t)count
{
	size_t size, lastPageByte;

	if (count > SIZE_MAX - _count || _count + count > SIZE_MAX / _itemSize)
		@throw [OFOutOfRangeException exception];




	lastPageByte = [OFSystemInfo pageSize] - 1;
	size = ((_count + count) * _itemSize + lastPageByte) & ~lastPageByte;


	if (size > _capacity) {
		_items = [self resizeMemory: _items
				       size: size];

		_capacity = size;

	}

	memcpy(_items + _count * _itemSize, items, count * _itemSize);

	_count += count;
	_size = size;
}

- (void)insertItems: (const void*)items
	    atIndex: (size_t)index
	      count: (size_t)count
{
	size_t size, lastPageByte;

	if (count > SIZE_MAX - _count || index > _count ||
	    _count + count > SIZE_MAX / _itemSize)
		@throw [OFOutOfRangeException exception];




	lastPageByte = [OFSystemInfo pageSize] - 1;
	size = ((_count + count) * _itemSize + lastPageByte) & ~lastPageByte;


	if (size > _capacity) {
		_items = [self resizeMemory: _items
				       size: size];

		_capacity = size;

	}

	memmove(_items + (index + count) * _itemSize,
	    _items + index * _itemSize, (_count - index) * _itemSize);
	memcpy(_items + index * _itemSize, items, count * _itemSize);

	_count += count;
	_size = size;
}

- (void)removeItemsInRange: (of_range_t)range
{
	size_t pageSize, size;

	if (range.length > SIZE_MAX - range.location ||
	    range.location + range.length > _count)
		@throw [OFOutOfRangeException exception];

	memmove(_items + range.location * _itemSize,
	    _items + (range.location + range.length) * _itemSize,
	    (_count - range.location - range.length) * _itemSize);

	_count -= range.length;
	pageSize = [OFSystemInfo pageSize];
	size = (_count * _itemSize + pageSize - 1) & ~(pageSize - 1);

	if (_size != size && size >= pageSize) {
		@try {
			_items = [self resizeMemory: _items
					       size: size];
			_capacity = size;

		} @catch (OFOutOfMemoryException *e) {
			/* We don't care, as we only made it smaller */
		}

		_size = size;
	}
}

- (void)removeLastItem
{
	size_t pageSize, size;

	if (_count == 0)
		return;

	_count--;
	pageSize = [OFSystemInfo pageSize];
	size = (_count * _itemSize + pageSize - 1) & ~(pageSize - 1);

	if (_size != size && size >= pageSize) {
		@try {
			_items = [self resizeMemory: _items
					       size: size];
			_capacity = size;

		} @catch (OFOutOfMemoryException *e) {
			/* We don't care, as we only made it smaller */
		}

		_size = size;
	}
}

- (void)removeAllItems
{
	size_t pageSize = [OFSystemInfo pageSize];

	@try {
		_items = [self resizeMemory: _items
				       size: pageSize];
		_capacity = pageSize;
		_size = pageSize;
	} @catch (OFOutOfMemoryException *e) {
		/* We don't care, as we only made it smaller */
	}

	_count = 0;
}
@end







>
>
>
>
>
>

>
>
>
>
>
>


>
|
>
>

>
>
>
>
>
>
>
>
>
|

>
>
|
>
|
>
>
>
>
>
>
>
>
>




<
<



>
>
>
|
|
>

<


>
|
>





<





<
<



>
>
>
|
|
>

<


>
|
>





<






<
<




>
>
>
|
|
>

<


>
|
>







<




|










|
|

|



|
>



<
<





|





|
|

|



|
>



<
<










|








676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731


732
733
734
735
736
737
738
739
740
741

742
743
744
745
746
747
748
749
750
751

752
753
754
755
756


757
758
759
760
761
762
763
764
765
766

767
768
769
770
771
772
773
774
775
776

777
778
779
780
781
782


783
784
785
786
787
788
789
790
791
792
793

794
795
796
797
798
799
800
801
802
803
804
805

806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832


833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855


856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
		 count: _count];

	return data;
}
@end

@implementation OFBigDataArray
- init
{
	return [self initWithItemSize: 1
			     capacity: 0];
}

- initWithItemSize: (size_t)itemSize
{
	return [self initWithItemSize: itemSize
			     capacity: 0];
}

- initWithItemSize: (size_t)itemSize
	  capacity: (size_t)capacity
{
	self = [super init];

	@try {
		size_t size, lastPageByte;

		if (itemSize == 0) {
			[self release];
			@throw [OFInvalidArgumentException exception];
		}

		if (capacity > SIZE_MAX / itemSize)
			@throw [OFOutOfRangeException exception];

		lastPageByte = [OFSystemInfo pageSize] - 1;
		size = (capacity * itemSize + lastPageByte) & ~lastPageByte;

		if (size == 0)
			size = lastPageByte + 1;

		_items = [self allocMemoryWithSize: size];

		_itemSize = itemSize;
		_capacity = size / itemSize;
		_size = size;
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

- (void)addItem: (const void*)item
{


	if (SIZE_MAX - _count < 1 || _count + 1 > SIZE_MAX / _itemSize)
		@throw [OFOutOfRangeException exception];

	if (_count + 1 > _capacity) {
		size_t size, lastPageByte;

		lastPageByte = [OFSystemInfo pageSize] - 1;
		size = ((_count + 1) * _itemSize + lastPageByte) &
		    ~lastPageByte;


		_items = [self resizeMemory: _items
				       size: size];

		_capacity = size / _itemSize;
		_size = size;
	}

	memcpy(_items + _count * _itemSize, item, _itemSize);

	_count++;

}

- (void)addItems: (const void*)items
	   count: (size_t)count
{


	if (count > SIZE_MAX - _count || _count + count > SIZE_MAX / _itemSize)
		@throw [OFOutOfRangeException exception];

	if (_count + count > _capacity) {
		size_t size, lastPageByte;

		lastPageByte = [OFSystemInfo pageSize] - 1;
		size = ((_count + count) * _itemSize + lastPageByte) &
		    ~lastPageByte;


		_items = [self resizeMemory: _items
				       size: size];

		_capacity = size / _itemSize;
		_size = size;
	}

	memcpy(_items + _count * _itemSize, items, count * _itemSize);

	_count += count;

}

- (void)insertItems: (const void*)items
	    atIndex: (size_t)index
	      count: (size_t)count
{


	if (count > SIZE_MAX - _count || index > _count ||
	    _count + count > SIZE_MAX / _itemSize)
		@throw [OFOutOfRangeException exception];

	if (_count + count > _capacity) {
		size_t size, lastPageByte;

		lastPageByte = [OFSystemInfo pageSize] - 1;
		size = ((_count + count) * _itemSize + lastPageByte) &
		    ~lastPageByte;


		_items = [self resizeMemory: _items
				       size: size];

		_capacity = size / _itemSize;
		_size = size;
	}

	memmove(_items + (index + count) * _itemSize,
	    _items + index * _itemSize, (_count - index) * _itemSize);
	memcpy(_items + index * _itemSize, items, count * _itemSize);

	_count += count;

}

- (void)removeItemsInRange: (of_range_t)range
{
	size_t lastPageByte, size;

	if (range.length > SIZE_MAX - range.location ||
	    range.location + range.length > _count)
		@throw [OFOutOfRangeException exception];

	memmove(_items + range.location * _itemSize,
	    _items + (range.location + range.length) * _itemSize,
	    (_count - range.location - range.length) * _itemSize);

	_count -= range.length;
	lastPageByte = [OFSystemInfo pageSize] - 1;
	size = (_count * _itemSize + lastPageByte) & ~lastPageByte;

	if (_size != size && size > lastPageByte) {
		@try {
			_items = [self resizeMemory: _items
					       size: size];
			_capacity = size / _itemSize;
			_size = size;
		} @catch (OFOutOfMemoryException *e) {
			/* We don't care, as we only made it smaller */
		}


	}
}

- (void)removeLastItem
{
	size_t lastPageByte, size;

	if (_count == 0)
		return;

	_count--;
	lastPageByte = [OFSystemInfo pageSize] - 1;
	size = (_count * _itemSize + lastPageByte) & ~lastPageByte;

	if (_size != size && size > lastPageByte) {
		@try {
			_items = [self resizeMemory: _items
					       size: size];
			_capacity = size / _itemSize;
			_size = size;
		} @catch (OFOutOfMemoryException *e) {
			/* We don't care, as we only made it smaller */
		}


	}
}

- (void)removeAllItems
{
	size_t pageSize = [OFSystemInfo pageSize];

	@try {
		_items = [self resizeMemory: _items
				       size: pageSize];
		_capacity = pageSize / _itemSize;
		_size = pageSize;
	} @catch (OFOutOfMemoryException *e) {
		/* We don't care, as we only made it smaller */
	}

	_count = 0;
}
@end