ObjFW  Check-in [2547c1c4ce]

Overview
Comment:Make sure objects are correctly aligned on Windows

malloc() on Windows XP only aligns with 8 bytes, which is not enough for
SSE.

Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 2547c1c4ce5005680e9c940036a9322e5e0fdaed1f5f16558158dcfae72369b1
User & Date: js on 2024-02-13 22:40:37
Other Links: manifest | tags
Context
2024-02-18
13:53
OFMapTable: Improve enumeration mutation detection check-in: ab14af79d2 user: js tags: trunk
2024-02-13
22:43
Merge trunk into branch "objfwtest" check-in: ac3ec432a6 user: js tags: objfwtest
22:40
Make sure objects are correctly aligned on Windows check-in: 2547c1c4ce user: js tags: trunk
22:06
Don't use max_align_t for OF_BIGGEST_ALIGNMENT check-in: c80075f6e4 user: js tags: trunk
Changes

Modified src/OFObject.m from [70b0e206af] to [8335b4d9d4].

83
84
85
86
87
88
89
90
91


92
93
94
95
96
97
98
83
84
85
86
87
88
89


90
91
92
93
94
95
96
97
98







-
-
+
+







struct PreIvars {
	int retainCount;
#if !defined(OF_HAVE_ATOMIC_OPS) && !defined(OF_AMIGAOS)
	OFSpinlock retainCountSpinlock;
#endif
};

#define PRE_IVARS_ALIGN ((sizeof(struct PreIvars) + \
    (OF_BIGGEST_ALIGNMENT - 1)) & ~(OF_BIGGEST_ALIGNMENT - 1))
#define PRE_IVARS_ALIGN \
	OFRoundUpToPowerOf2(sizeof(struct PreIvars), OF_BIGGEST_ALIGNMENT)
#define PRE_IVARS ((struct PreIvars *)(void *)((char *)self - PRE_IVARS_ALIGN))

static struct {
	Class isa;
} allocFailedException;

unsigned long OFHashSeed;
336
337
338
339
340
341
342

343
344






345
346
347
348
349
350
351
352
353
354
355
356

357



358
359
360
361
362
363
364
365
366
367
368
369

370



371
372
373
374
375
376
377
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392







+


+
+
+
+
+
+












+

+
+
+












+

+
+
+







	instanceSize = class_getInstanceSize(class);

	if OF_UNLIKELY (extraAlignment > 1)
		extraAlignment = OFRoundUpToPowerOf2(extraAlignment,
		    PRE_IVARS_ALIGN + instanceSize) -
		    PRE_IVARS_ALIGN - instanceSize;

#ifndef OF_WINDOWS
	instance = calloc(1, PRE_IVARS_ALIGN + instanceSize +
	    extraAlignment + extraSize);
#else
	instance = _aligned_malloc(PRE_IVARS_ALIGN + instanceSize +
	    extraAlignment + extraSize, OF_BIGGEST_ALIGNMENT);
	memset(instance, 0, PRE_IVARS_ALIGN + instanceSize + extraAlignment +
	    extraSize);
#endif

	if OF_UNLIKELY (instance == nil) {
		object_setClass((id)&allocFailedException,
		    [OFAllocFailedException class]);
		@throw (id)&allocFailedException;
	}

	((struct PreIvars *)instance)->retainCount = 1;

#if !defined(OF_HAVE_ATOMIC_OPS) && !defined(OF_AMIGAOS)
	if OF_UNLIKELY (OFSpinlockNew(
	    &((struct PreIvars *)instance)->retainCountSpinlock) != 0) {
# ifndef OF_WINDOWS
		free(instance);
# else
		_aligned_free(instance);
# endif
		@throw [OFInitializationFailedException
		    exceptionWithClass: class];
	}
#endif

	instance = (OFObject *)(void *)((char *)instance + PRE_IVARS_ALIGN);

	if (!objc_constructInstance(class, instance)) {
#if !defined(OF_HAVE_ATOMIC_OPS) && !defined(OF_AMIGAOS)
		OFSpinlockFree(&((struct PreIvars *)(void *)
		    ((char *)instance - PRE_IVARS_ALIGN))->retainCountSpinlock);
#endif
#ifndef OF_WINDOWS
		free((char *)instance - PRE_IVARS_ALIGN);
#else
		_aligned_free((char *)instance - PRE_IVARS_ALIGN);
#endif
		@throw [OFInitializationFailedException
		    exceptionWithClass: class];
	}

	if OF_UNLIKELY (extra != NULL)
		*extra = (char *)instance + instanceSize + extraAlignment;

1230
1231
1232
1233
1234
1235
1236

1237



1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263







+

+
+
+







{
	objc_destructInstance(self);

#if !defined(OF_HAVE_ATOMIC_OPS) && !defined(OF_AMIGAOS)
	OFSpinlockFree(&PRE_IVARS->retainCountSpinlock);
#endif

#ifndef OF_WINDOWS
	free((char *)self - PRE_IVARS_ALIGN);
#else
	_aligned_free((char *)self - PRE_IVARS_ALIGN);
#endif
}

/* Required to use properties with the Apple runtime */
- (id)copyWithZone: (void *)zone
{
	if OF_UNLIKELY (zone != NULL) {
		[self doesNotRecognizeSelector: _cmd];