ObjFW  Check-in [3005748a9d]

Overview
Comment:One malloc less when allocating a string.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 3005748a9d440acd4ee2e033f3354f24795c8bcd035c81b315f369b1f4850ff3
User & Date: js on 2012-03-16 14:01:29
Other Links: manifest | tags
Context
2012-03-16
17:14
Fix memory wasting in OFBigDataArray. check-in: 36872c7a25 user: js tags: trunk
14:01
One malloc less when allocating a string. check-in: 3005748a9d user: js tags: trunk
2012-03-15
11:29
Greatly improve OFObject's memory handling and performance. check-in: 28170f5f65 user: js tags: trunk
Changes

Modified src/OFMutableString_UTF8.h from [593b73ce69] to [ddae683c1b].

11
12
13
14
15
16
17

18
19
20
21
22

23
24
 * Alternatively, it may be distributed under the terms of the GNU General
 * Public License, either version 2 or 3, which can be found in the file
 * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this
 * file.
 */

#import "OFMutableString.h"


@interface OFMutableString_UTF8: OFMutableString
{
@public
	struct of_string_utf8_ivars *restrict s;

}
@end







>





>


11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
 * Alternatively, it may be distributed under the terms of the GNU General
 * Public License, either version 2 or 3, which can be found in the file
 * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this
 * file.
 */

#import "OFMutableString.h"
#import "OFString_UTF8.h"

@interface OFMutableString_UTF8: OFMutableString
{
@public
	struct of_string_utf8_ivars *restrict s;
	struct of_string_utf8_ivars s_store;
}
@end

Modified src/OFString_UTF8.h from [958573e473] to [b1a5c898cd].

15
16
17
18
19
20
21







22
23
24
25
26
27
28
29

30
31
 */

#import "OFString.h"

@interface OFString_UTF8: OFString
{
@public







	struct of_string_utf8_ivars {
		char	 *cString;
		size_t	 cStringLength;
		BOOL	 UTF8;
		size_t	 length;
		BOOL	 hashed;
		uint32_t hash;
	} *restrict s;

}
@end







>
>
>
>
>
>
>








>


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
 */

#import "OFString.h"

@interface OFString_UTF8: OFString
{
@public
	/*
	 * A pointer to the actual data.
	 *
	 * Since constant strings don't have s_store, they have to malloc it on
	 * the first access. Strings created at runtime just set the pointer to
	 * &s_store.
	 */
	struct of_string_utf8_ivars {
		char	 *cString;
		size_t	 cStringLength;
		BOOL	 UTF8;
		size_t	 length;
		BOOL	 hashed;
		uint32_t hash;
	} *restrict s;
	struct of_string_utf8_ivars s_store;
}
@end

Modified src/OFString_UTF8.m from [2df9fbd0df] to [2441b116ae].

60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75

@implementation OFString_UTF8
- init
{
	self = [super init];

	@try {
		s = [self allocMemoryWithSize: sizeof(*s)];
		memset(s, 0, sizeof(*s));

		s->cString = [self allocMemoryWithSize: 1];
		s->cString[0] = '\0';
	} @catch (id e) {
		[self release];
		@throw e;
	}







|
<







60
61
62
63
64
65
66
67

68
69
70
71
72
73
74

@implementation OFString_UTF8
- init
{
	self = [super init];

	@try {
		s = &s_store;


		s->cString = [self allocMemoryWithSize: 1];
		s->cString[0] = '\0';
	} @catch (id e) {
		[self release];
		@throw e;
	}
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104

		if (encoding == OF_STRING_ENCODING_UTF_8 &&
		    cStringLength >= 3 && !memcmp(cString, "\xEF\xBB\xBF", 3)) {
			cString += 3;
			cStringLength -= 3;
		}

		s = [self allocMemoryWithSize: sizeof(*s)];
		memset(s, 0, sizeof(*s));

		s->cString = [self allocMemoryWithSize: cStringLength + 1];
		s->cStringLength = cStringLength;

		if (encoding == OF_STRING_ENCODING_UTF_8 ||
		    encoding == OF_STRING_ENCODING_ASCII) {
			switch (of_string_check_utf8(cString, cStringLength,







|
<







88
89
90
91
92
93
94
95

96
97
98
99
100
101
102

		if (encoding == OF_STRING_ENCODING_UTF_8 &&
		    cStringLength >= 3 && !memcmp(cString, "\xEF\xBB\xBF", 3)) {
			cString += 3;
			cStringLength -= 3;
		}

		s = &s_store;


		s->cString = [self allocMemoryWithSize: cStringLength + 1];
		s->cStringLength = cStringLength;

		if (encoding == OF_STRING_ENCODING_UTF_8 ||
		    encoding == OF_STRING_ENCODING_ASCII) {
			switch (of_string_check_utf8(cString, cStringLength,
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
}

- initWithString: (OFString*)string
{
	self = [super init];

	@try {
		s = [self allocMemoryWithSize: sizeof(*s)];
		memset(s, 0, sizeof(*s));

		s->cStringLength = [string UTF8StringLength];

		if ([string isKindOfClass: [OFString_UTF8 class]] ||
		    [string isKindOfClass: [OFMutableString_UTF8 class]])
			s->UTF8 = ((OFString_UTF8*)string)->s->UTF8;
		else







|
<







208
209
210
211
212
213
214
215

216
217
218
219
220
221
222
}

- initWithString: (OFString*)string
{
	self = [super init];

	@try {
		s = &s_store;


		s->cStringLength = [string UTF8StringLength];

		if ([string isKindOfClass: [OFString_UTF8 class]] ||
		    [string isKindOfClass: [OFMutableString_UTF8 class]])
			s->UTF8 = ((OFString_UTF8*)string)->s->UTF8;
		else
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
		} else if (length > 0 && *string == 0xFFFE0000) {
			swap = YES;
			string++;
			length--;
		} else if (byteOrder != OF_ENDIANESS_NATIVE)
			swap = YES;

		s = [self allocMemoryWithSize: sizeof(*s)];
		memset(s, 0, sizeof(*s));

		s->cStringLength = length;
		s->cString = [self allocMemoryWithSize: (length * 4) + 1];
		s->length = length;

		for (i = 0; i < length; i++) {
			char buffer[4];







|
<







250
251
252
253
254
255
256
257

258
259
260
261
262
263
264
		} else if (length > 0 && *string == 0xFFFE0000) {
			swap = YES;
			string++;
			length--;
		} else if (byteOrder != OF_ENDIANESS_NATIVE)
			swap = YES;

		s = &s_store;


		s->cStringLength = length;
		s->cString = [self allocMemoryWithSize: (length * 4) + 1];
		s->length = length;

		for (i = 0; i < length; i++) {
			char buffer[4];
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
		} else if (length > 0 && *string == 0xFFFE) {
			swap = YES;
			string++;
			length--;
		} else if (byteOrder != OF_ENDIANESS_NATIVE)
			swap = YES;

		s = [self allocMemoryWithSize: sizeof(*s)];
		memset(s, 0, sizeof(*s));

		s->cStringLength = length;
		s->cString = [self allocMemoryWithSize: (length * 4) + 1];
		s->length = length;

		for (i = 0; i < length; i++) {
			char buffer[4];







|
<







332
333
334
335
336
337
338
339

340
341
342
343
344
345
346
		} else if (length > 0 && *string == 0xFFFE) {
			swap = YES;
			string++;
			length--;
		} else if (byteOrder != OF_ENDIANESS_NATIVE)
			swap = YES;

		s = &s_store;


		s->cStringLength = length;
		s->cString = [self allocMemoryWithSize: (length * 4) + 1];
		s->length = length;

		for (i = 0; i < length; i++) {
			char buffer[4];
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
		int cStringLength;

		if (format == nil)
			@throw [OFInvalidArgumentException
			    exceptionWithClass: isa
				      selector: _cmd];

		s = [self allocMemoryWithSize: sizeof(*s)];
		memset(s, 0, sizeof(*s));

		if ((cStringLength = of_vasprintf(&tmp, [format UTF8String],
		    arguments)) == -1)
			@throw [OFInvalidFormatException
			    exceptionWithClass: isa];

		s->cStringLength = cStringLength;







|
<







434
435
436
437
438
439
440
441

442
443
444
445
446
447
448
		int cStringLength;

		if (format == nil)
			@throw [OFInvalidArgumentException
			    exceptionWithClass: isa
				      selector: _cmd];

		s = &s_store;


		if ((cStringLength = of_vasprintf(&tmp, [format UTF8String],
		    arguments)) == -1)
			@throw [OFInvalidFormatException
			    exceptionWithClass: isa];

		s->cStringLength = cStringLength;
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
	self = [super init];

	@try {
		OFString *component;
		size_t i, cStringLength;
		va_list argumentsCopy;

		s = [self allocMemoryWithSize: sizeof(*s)];
		memset(s, 0, sizeof(*s));

		s->cStringLength = [firstComponent UTF8StringLength];

		if ([firstComponent isKindOfClass: [OFString_UTF8 class]] ||
		    [firstComponent isKindOfClass:
		    [OFMutableString_UTF8 class]])
			s->UTF8 = ((OFString_UTF8*)firstComponent)->s->UTF8;







|
<







478
479
480
481
482
483
484
485

486
487
488
489
490
491
492
	self = [super init];

	@try {
		OFString *component;
		size_t i, cStringLength;
		va_list argumentsCopy;

		s = &s_store;


		s->cStringLength = [firstComponent UTF8StringLength];

		if ([firstComponent isKindOfClass: [OFString_UTF8 class]] ||
		    [firstComponent isKindOfClass:
		    [OFMutableString_UTF8 class]])
			s->UTF8 = ((OFString_UTF8*)firstComponent)->s->UTF8;