Commit bc5b19e60406b2b0b7173d866a808313fca2b4ce

Patrick Steinhardt 2019-04-29T09:01:45

Merge pull request #4561 from pks-t/pks/downcasting [RFC] util: introduce GIT_DOWNCAST macro

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
diff --git a/src/config_file.c b/src/config_file.c
index 7f8dd65..a36c0aa 100644
--- a/src/config_file.c
+++ b/src/config_file.c
@@ -227,9 +227,9 @@ static void backend_free(git_config_backend *_backend)
 
 static int config_iterator_new(
 	git_config_iterator **iter,
-	struct git_config_backend* backend)
+	struct git_config_backend *backend)
 {
-	diskfile_header *bh = (diskfile_header *) backend;
+	diskfile_header *bh = GIT_CONTAINER_OF(backend, diskfile_header, parent);
 	git_config_entries *entries;
 	int error;
 
diff --git a/src/iterator.c b/src/iterator.c
index 0db9292..d00b8aa 100644
--- a/src/iterator.c
+++ b/src/iterator.c
@@ -1521,7 +1521,7 @@ static void filesystem_iterator_set_current(
 static int filesystem_iterator_current(
 	const git_index_entry **out, git_iterator *i)
 {
-	filesystem_iterator *iter = (filesystem_iterator *)i;
+	filesystem_iterator *iter = GIT_CONTAINER_OF(i, filesystem_iterator, base);
 
 	if (!iterator__has_been_accessed(i))
 		return iter->base.cb->advance(out, i);
@@ -1568,7 +1568,7 @@ done:
 static int filesystem_iterator_advance(
 	const git_index_entry **out, git_iterator *i)
 {
-	filesystem_iterator *iter = (filesystem_iterator *)i;
+	filesystem_iterator *iter = GIT_CONTAINER_OF(i, filesystem_iterator, base);
 	bool is_dir;
 	int error = 0;
 
@@ -1627,7 +1627,7 @@ static int filesystem_iterator_advance(
 static int filesystem_iterator_advance_into(
 	const git_index_entry **out, git_iterator *i)
 {
-	filesystem_iterator *iter = (filesystem_iterator *)i;
+	filesystem_iterator *iter = GIT_CONTAINER_OF(i, filesystem_iterator, base);
 	filesystem_iterator_frame *frame;
 	filesystem_iterator_entry *prev_entry;
 	int error;
@@ -1664,7 +1664,7 @@ static int filesystem_iterator_advance_into(
 
 int git_iterator_current_workdir_path(git_buf **out, git_iterator *i)
 {
-	filesystem_iterator *iter = (filesystem_iterator *)i;
+	filesystem_iterator *iter = GIT_CONTAINER_OF(i, filesystem_iterator, base);
 	const git_index_entry *entry;
 
 	if (i->type != GIT_ITERATOR_TYPE_FS &&
@@ -1724,15 +1724,19 @@ GIT_INLINE(bool) filesystem_iterator_current_is_ignored(
 
 bool git_iterator_current_is_ignored(git_iterator *i)
 {
+	filesystem_iterator *iter = NULL;
+
 	if (i->type != GIT_ITERATOR_TYPE_WORKDIR)
 		return false;
 
-	return filesystem_iterator_current_is_ignored((filesystem_iterator *)i);
+	iter = GIT_CONTAINER_OF(i, filesystem_iterator, base);
+
+	return filesystem_iterator_current_is_ignored(iter);
 }
 
 bool git_iterator_current_tree_is_ignored(git_iterator *i)
 {
-	filesystem_iterator *iter = (filesystem_iterator *)i;
+	filesystem_iterator *iter = GIT_CONTAINER_OF(i, filesystem_iterator, base);
 	filesystem_iterator_frame *frame;
 
 	if (i->type != GIT_ITERATOR_TYPE_WORKDIR)
@@ -1747,7 +1751,7 @@ static int filesystem_iterator_advance_over(
 	git_iterator_status_t *status,
 	git_iterator *i)
 {
-	filesystem_iterator *iter = (filesystem_iterator *)i;
+	filesystem_iterator *iter = GIT_CONTAINER_OF(i, filesystem_iterator, base);
 	filesystem_iterator_frame *current_frame;
 	filesystem_iterator_entry *current_entry;
 	const git_index_entry *entry = NULL;
@@ -1866,7 +1870,7 @@ static int filesystem_iterator_init(filesystem_iterator *iter)
 
 static int filesystem_iterator_reset(git_iterator *i)
 {
-	filesystem_iterator *iter = (filesystem_iterator *)i;
+	filesystem_iterator *iter = GIT_CONTAINER_OF(i, filesystem_iterator, base);
 
 	filesystem_iterator_clear(iter);
 	return filesystem_iterator_init(iter);
@@ -1874,7 +1878,7 @@ static int filesystem_iterator_reset(git_iterator *i)
 
 static void filesystem_iterator_free(git_iterator *i)
 {
-	filesystem_iterator *iter = (filesystem_iterator *)i;
+	filesystem_iterator *iter = GIT_CONTAINER_OF(i, filesystem_iterator, base);
 	git__free(iter->root);
 	git_buf_dispose(&iter->current_path);
 	git_tree_free(iter->tree);
@@ -2083,7 +2087,7 @@ static int index_iterator_skip_pseudotree(index_iterator *iter)
 static int index_iterator_advance(
 	const git_index_entry **out, git_iterator *i)
 {
-	index_iterator *iter = (index_iterator *)i;
+	index_iterator *iter = GIT_CONTAINER_OF(i, index_iterator, base);
 	const git_index_entry *entry = NULL;
 	bool is_submodule;
 	int error = 0;
@@ -2156,7 +2160,7 @@ static int index_iterator_advance(
 static int index_iterator_advance_into(
 	const git_index_entry **out, git_iterator *i)
 {
-	index_iterator *iter = (index_iterator *)i;
+	index_iterator *iter = GIT_CONTAINER_OF(i, index_iterator, base);
 
 	if (! S_ISDIR(iter->tree_entry.mode)) {
 		if (out)
@@ -2174,7 +2178,7 @@ static int index_iterator_advance_over(
 	git_iterator_status_t *status,
 	git_iterator *i)
 {
-	index_iterator *iter = (index_iterator *)i;
+	index_iterator *iter = GIT_CONTAINER_OF(i, index_iterator, base);
 	const git_index_entry *entry;
 	int error;
 
@@ -2203,7 +2207,7 @@ static int index_iterator_init(index_iterator *iter)
 
 static int index_iterator_reset(git_iterator *i)
 {
-	index_iterator *iter = (index_iterator *)i;
+	index_iterator *iter = GIT_CONTAINER_OF(i, index_iterator, base);
 
 	index_iterator_clear(iter);
 	return index_iterator_init(iter);
@@ -2211,7 +2215,7 @@ static int index_iterator_reset(git_iterator *i)
 
 static void index_iterator_free(git_iterator *i)
 {
-	index_iterator *iter = (index_iterator *)i;
+	index_iterator *iter = GIT_CONTAINER_OF(i, index_iterator, base);
 
 	git_index_snapshot_release(&iter->entries, iter->base.index);
 	git_buf_dispose(&iter->tree_buf);
diff --git a/src/refdb_fs.c b/src/refdb_fs.c
index 30aa1a8..c8533e6 100644
--- a/src/refdb_fs.c
+++ b/src/refdb_fs.c
@@ -327,7 +327,7 @@ static int refdb_fs_backend__exists(
 	git_refdb_backend *_backend,
 	const char *ref_name)
 {
-	refdb_fs_backend *backend = (refdb_fs_backend *)_backend;
+	refdb_fs_backend *backend = GIT_CONTAINER_OF(_backend, refdb_fs_backend, parent);
 	git_buf ref_path = GIT_BUF_INIT;
 	int error;
 
@@ -469,7 +469,7 @@ static int refdb_fs_backend__lookup(
 	git_refdb_backend *_backend,
 	const char *ref_name)
 {
-	refdb_fs_backend *backend = (refdb_fs_backend *)_backend;
+	refdb_fs_backend *backend = GIT_CONTAINER_OF(_backend, refdb_fs_backend, parent);
 	int error;
 
 	assert(backend);
@@ -502,7 +502,7 @@ typedef struct {
 
 static void refdb_fs_backend__iterator_free(git_reference_iterator *_iter)
 {
-	refdb_fs_iter *iter = (refdb_fs_iter *) _iter;
+	refdb_fs_iter *iter = GIT_CONTAINER_OF(_iter, refdb_fs_iter, parent);
 
 	git_vector_free(&iter->loose);
 	git_pool_clear(&iter->pool);
@@ -591,8 +591,8 @@ static int refdb_fs_backend__iterator_next(
 	git_reference **out, git_reference_iterator *_iter)
 {
 	int error = GIT_ITEROVER;
-	refdb_fs_iter *iter = (refdb_fs_iter *)_iter;
-	refdb_fs_backend *backend = (refdb_fs_backend *)iter->parent.db->backend;
+	refdb_fs_iter *iter = GIT_CONTAINER_OF(_iter, refdb_fs_iter, parent);
+	refdb_fs_backend *backend = GIT_CONTAINER_OF(iter->parent.db->backend, refdb_fs_backend, parent);
 	struct packref *ref;
 
 	while (iter->loose_pos < iter->loose.length) {
@@ -632,8 +632,8 @@ static int refdb_fs_backend__iterator_next_name(
 	const char **out, git_reference_iterator *_iter)
 {
 	int error = GIT_ITEROVER;
-	refdb_fs_iter *iter = (refdb_fs_iter *)_iter;
-	refdb_fs_backend *backend = (refdb_fs_backend *)iter->parent.db->backend;
+	refdb_fs_iter *iter = GIT_CONTAINER_OF(_iter, refdb_fs_iter, parent);
+	refdb_fs_backend *backend = GIT_CONTAINER_OF(iter->parent.db->backend, refdb_fs_backend, parent);
 	struct packref *ref;
 
 	while (iter->loose_pos < iter->loose.length) {
@@ -674,7 +674,7 @@ static int refdb_fs_backend__iterator_next_name(
 static int refdb_fs_backend__iterator(
 	git_reference_iterator **out, git_refdb_backend *_backend, const char *glob)
 {
-	refdb_fs_backend *backend = (refdb_fs_backend *)_backend;
+	refdb_fs_backend *backend = GIT_CONTAINER_OF(_backend, refdb_fs_backend, parent);
 	refdb_fs_iter *iter = NULL;
 	int error;
 
@@ -838,7 +838,7 @@ static int refdb_fs_backend__lock(void **out, git_refdb_backend *_backend, const
 {
 	int error;
 	git_filebuf *lock;
-	refdb_fs_backend *backend = (refdb_fs_backend *) _backend;
+	refdb_fs_backend *backend = GIT_CONTAINER_OF(_backend, refdb_fs_backend, parent);
 
 	lock = git__calloc(1, sizeof(git_filebuf));
 	GIT_ERROR_CHECK_ALLOC(lock);
@@ -1248,7 +1248,7 @@ static int refdb_fs_backend__write(
 	const git_oid *old_id,
 	const char *old_target)
 {
-	refdb_fs_backend *backend = (refdb_fs_backend *)_backend;
+	refdb_fs_backend *backend = GIT_CONTAINER_OF(_backend, refdb_fs_backend, parent);
 	git_filebuf file = GIT_FILEBUF_INIT;
 	int error = 0;
 
@@ -1274,7 +1274,7 @@ static int refdb_fs_backend__write_tail(
 	const git_oid *old_id,
 	const char *old_target)
 {
-	refdb_fs_backend *backend = (refdb_fs_backend *)_backend;
+	refdb_fs_backend *backend = GIT_CONTAINER_OF(_backend, refdb_fs_backend, parent);
 	int error = 0, cmp = 0, should_write;
 	const char *new_target = NULL;
 	const git_oid *new_id = NULL;
@@ -1364,7 +1364,7 @@ static int refdb_fs_backend__delete(
 	const char *ref_name,
 	const git_oid *old_id, const char *old_target)
 {
-	refdb_fs_backend *backend = (refdb_fs_backend *)_backend;
+	refdb_fs_backend *backend = GIT_CONTAINER_OF(_backend, refdb_fs_backend, parent);
 	git_filebuf file = GIT_FILEBUF_INIT;
 	int error = 0;
 
@@ -1387,7 +1387,7 @@ static int refdb_fs_backend__delete_tail(
 	const char *ref_name,
 	const git_oid *old_id, const char *old_target)
 {
-	refdb_fs_backend *backend = (refdb_fs_backend *)_backend;
+	refdb_fs_backend *backend = GIT_CONTAINER_OF(_backend, refdb_fs_backend, parent);
 	git_buf loose_path = GIT_BUF_INIT;
 	size_t pack_pos;
 	int error = 0, cmp = 0;
@@ -1455,7 +1455,7 @@ static int refdb_fs_backend__rename(
 	const git_signature *who,
 	const char *message)
 {
-	refdb_fs_backend *backend = (refdb_fs_backend *)_backend;
+	refdb_fs_backend *backend = GIT_CONTAINER_OF(_backend, refdb_fs_backend, parent);
 	git_reference *old, *new;
 	git_filebuf file = GIT_FILEBUF_INIT;
 	int error;
@@ -1511,7 +1511,7 @@ static int refdb_fs_backend__rename(
 static int refdb_fs_backend__compress(git_refdb_backend *_backend)
 {
 	int error;
-	refdb_fs_backend *backend = (refdb_fs_backend *)_backend;
+	refdb_fs_backend *backend = GIT_CONTAINER_OF(_backend, refdb_fs_backend, parent);
 
 	assert(backend);
 
@@ -1525,7 +1525,7 @@ static int refdb_fs_backend__compress(git_refdb_backend *_backend)
 
 static void refdb_fs_backend__free(git_refdb_backend *_backend)
 {
-	refdb_fs_backend *backend = (refdb_fs_backend *)_backend;
+	refdb_fs_backend *backend = GIT_CONTAINER_OF(_backend, refdb_fs_backend, parent);
 
 	assert(backend);
 
@@ -1703,7 +1703,7 @@ static int refdb_reflog_fs__ensure_log(git_refdb_backend *_backend, const char *
 
 	assert(_backend && name);
 
-	backend = (refdb_fs_backend *) _backend;
+	backend = GIT_CONTAINER_OF(_backend, refdb_fs_backend, parent);
 	repo = backend->repo;
 
 	if ((error = retrieve_reflog_path(&path, repo, name)) < 0)
@@ -1736,7 +1736,7 @@ static int refdb_reflog_fs__has_log(git_refdb_backend *_backend, const char *nam
 
 	assert(_backend && name);
 
-	backend = (refdb_fs_backend *) _backend;
+	backend = GIT_CONTAINER_OF(_backend, refdb_fs_backend, parent);
 
 	return has_reflog(backend->repo, name);
 }
@@ -1752,7 +1752,7 @@ static int refdb_reflog_fs__read(git_reflog **out, git_refdb_backend *_backend, 
 
 	assert(out && _backend && name);
 
-	backend = (refdb_fs_backend *) _backend;
+	backend = GIT_CONTAINER_OF(_backend, refdb_fs_backend, parent);
 	repo = backend->repo;
 
 	if (reflog_alloc(&log, name) < 0)
@@ -1862,7 +1862,7 @@ static int refdb_reflog_fs__write(git_refdb_backend *_backend, git_reflog *reflo
 
 	assert(_backend && reflog);
 
-	backend = (refdb_fs_backend *) _backend;
+	backend = GIT_CONTAINER_OF(_backend, refdb_fs_backend, parent);
 
 	if ((error = lock_reflog(&fbuf, backend, reflog->ref_name)) < 0)
 		return -1;
@@ -1984,7 +1984,7 @@ static int refdb_reflog_fs__rename(git_refdb_backend *_backend, const char *old_
 
 	assert(_backend && old_name && new_name);
 
-	backend = (refdb_fs_backend *) _backend;
+	backend = GIT_CONTAINER_OF(_backend, refdb_fs_backend, parent);
 	repo = backend->repo;
 
 	if ((error = git_reference__normalize_name(
@@ -2055,7 +2055,7 @@ cleanup:
 
 static int refdb_reflog_fs__delete(git_refdb_backend *_backend, const char *name)
 {
-	refdb_fs_backend *backend = (refdb_fs_backend *) _backend;
+	refdb_fs_backend *backend = GIT_CONTAINER_OF(_backend, refdb_fs_backend, parent);
 	git_buf path = GIT_BUF_INIT;
 	int error;
 
diff --git a/src/transports/http.c b/src/transports/http.c
index 80ba5ba..9d77e62 100644
--- a/src/transports/http.c
+++ b/src/transports/http.c
@@ -1144,7 +1144,7 @@ static int http_stream_write_chunked(
 	const char *buffer,
 	size_t len)
 {
-	http_stream *s = (http_stream *)stream;
+	http_stream *s = GIT_CONTAINER_OF(stream, http_stream, parent);
 	http_subtransport *t = OWNING_SUBTRANSPORT(s);
 
 	assert(t->connected);
@@ -1218,7 +1218,7 @@ static int http_stream_write_single(
 	const char *buffer,
 	size_t len)
 {
-	http_stream *s = (http_stream *)stream;
+	http_stream *s = GIT_CONTAINER_OF(stream, http_stream, parent);
 	http_subtransport *t = OWNING_SUBTRANSPORT(s);
 	git_buf request = GIT_BUF_INIT;
 
@@ -1252,7 +1252,7 @@ on_error:
 
 static void http_stream_free(git_smart_subtransport_stream *stream)
 {
-	http_stream *s = (http_stream *)stream;
+	http_stream *s = GIT_CONTAINER_OF(stream, http_stream, parent);
 
 	if (s->chunk_buffer)
 		git__free(s->chunk_buffer);
@@ -1365,7 +1365,7 @@ static int http_action(
 	const char *url,
 	git_smart_service_t action)
 {
-	http_subtransport *t = (http_subtransport *)subtransport;
+	http_subtransport *t = GIT_CONTAINER_OF(subtransport, http_subtransport, parent);
 	int ret;
 
 	assert(stream);
@@ -1419,7 +1419,7 @@ static void free_auth_contexts(git_vector *contexts)
 
 static int http_close(git_smart_subtransport *subtransport)
 {
-	http_subtransport *t = (http_subtransport *) subtransport;
+	http_subtransport *t = GIT_CONTAINER_OF(subtransport, http_subtransport, parent);
 
 	clear_parser_state(t);
 
@@ -1459,7 +1459,7 @@ static int http_close(git_smart_subtransport *subtransport)
 
 static void http_free(git_smart_subtransport *subtransport)
 {
-	http_subtransport *t = (http_subtransport *) subtransport;
+	http_subtransport *t = GIT_CONTAINER_OF(subtransport, http_subtransport, parent);
 
 	http_close(subtransport);
 
diff --git a/src/transports/smart.c b/src/transports/smart.c
index fd7113c..7f66ae0 100644
--- a/src/transports/smart.c
+++ b/src/transports/smart.c
@@ -63,7 +63,7 @@ static int git_smart__set_callbacks(
 	git_transport_certificate_check_cb certificate_check_cb,
 	void *message_cb_payload)
 {
-	transport_smart *t = (transport_smart *)transport;
+	transport_smart *t = GIT_CONTAINER_OF(transport, transport_smart, parent);
 
 	t->progress_cb = progress_cb;
 	t->error_cb = error_cb;
@@ -128,7 +128,7 @@ static int git_smart__set_custom_headers(
 	git_transport *transport,
 	const git_strarray *custom_headers)
 {
-	transport_smart *t = (transport_smart *)transport;
+	transport_smart *t = GIT_CONTAINER_OF(transport, transport_smart, parent);
 	size_t i;
 
 	if (t->custom_headers.count)
@@ -212,7 +212,7 @@ static int git_smart__connect(
 	int direction,
 	int flags)
 {
-	transport_smart *t = (transport_smart *)transport;
+	transport_smart *t = GIT_CONTAINER_OF(transport, transport_smart, parent);
 	git_smart_subtransport_stream *stream;
 	int error;
 	git_pkt *pkt;
@@ -315,7 +315,7 @@ cleanup:
 
 static int git_smart__ls(const git_remote_head ***out, size_t *size, git_transport *transport)
 {
-	transport_smart *t = (transport_smart *)transport;
+	transport_smart *t = GIT_CONTAINER_OF(transport, transport_smart, parent);
 
 	if (!t->have_refs) {
 		git_error_set(GIT_ERROR_NET, "the transport has not yet loaded the refs");
@@ -330,7 +330,7 @@ static int git_smart__ls(const git_remote_head ***out, size_t *size, git_transpo
 
 int git_smart__negotiation_step(git_transport *transport, void *data, size_t len)
 {
-	transport_smart *t = (transport_smart *)transport;
+	transport_smart *t = GIT_CONTAINER_OF(transport, transport_smart, parent);
 	git_smart_subtransport_stream *stream;
 	int error;
 
@@ -387,21 +387,21 @@ int git_smart__get_push_stream(transport_smart *t, git_smart_subtransport_stream
 
 static void git_smart__cancel(git_transport *transport)
 {
-	transport_smart *t = (transport_smart *)transport;
+	transport_smart *t = GIT_CONTAINER_OF(transport, transport_smart, parent);
 
 	git_atomic_set(&t->cancelled, 1);
 }
 
 static int git_smart__is_connected(git_transport *transport)
 {
-	transport_smart *t = (transport_smart *)transport;
+	transport_smart *t = GIT_CONTAINER_OF(transport, transport_smart, parent);
 
 	return t->connected;
 }
 
 static int git_smart__read_flags(git_transport *transport, int *flags)
 {
-	transport_smart *t = (transport_smart *)transport;
+	transport_smart *t = GIT_CONTAINER_OF(transport, transport_smart, parent);
 
 	*flags = t->flags;
 
@@ -410,7 +410,7 @@ static int git_smart__read_flags(git_transport *transport, int *flags)
 
 static int git_smart__close(git_transport *transport)
 {
-	transport_smart *t = (transport_smart *)transport;
+	transport_smart *t = GIT_CONTAINER_OF(transport, transport_smart, parent);
 	git_vector *common = &t->common;
 	unsigned int i;
 	git_pkt *p;
@@ -447,7 +447,7 @@ static int git_smart__close(git_transport *transport)
 
 static void git_smart__free(git_transport *transport)
 {
-	transport_smart *t = (transport_smart *)transport;
+	transport_smart *t = GIT_CONTAINER_OF(transport, transport_smart, parent);
 	git_vector *refs = &t->refs;
 	unsigned int i;
 	git_pkt *p;
@@ -479,7 +479,7 @@ static int ref_name_cmp(const void *a, const void *b)
 
 int git_transport_smart_certificate_check(git_transport *transport, git_cert *cert, int valid, const char *hostname)
 {
-	transport_smart *t = (transport_smart *)transport;
+	transport_smart *t = GIT_CONTAINER_OF(transport, transport_smart, parent);
 
 	assert(transport && cert && hostname);
 
@@ -491,7 +491,7 @@ int git_transport_smart_certificate_check(git_transport *transport, git_cert *ce
 
 int git_transport_smart_credentials(git_cred **out, git_transport *transport, const char *user, int methods)
 {
-	transport_smart *t = (transport_smart *)transport;
+	transport_smart *t = GIT_CONTAINER_OF(transport, transport_smart, parent);
 
 	assert(out && transport);
 
@@ -503,7 +503,7 @@ int git_transport_smart_credentials(git_cred **out, git_transport *transport, co
 
 int git_transport_smart_proxy_options(git_proxy_options *out, git_transport *transport)
 {
-	transport_smart *t = (transport_smart *) transport;
+	transport_smart *t = GIT_CONTAINER_OF(transport, transport_smart, parent);
 	return git_proxy_options_dup(out, &t->proxy);
 }
 
diff --git a/src/transports/ssh.c b/src/transports/ssh.c
index 5a6058f..9b5d8a5 100644
--- a/src/transports/ssh.c
+++ b/src/transports/ssh.c
@@ -132,7 +132,7 @@ static int ssh_stream_read(
 	size_t *bytes_read)
 {
 	int rc;
-	ssh_stream *s = (ssh_stream *)stream;
+	ssh_stream *s = GIT_CONTAINER_OF(stream, ssh_stream, parent);
 
 	*bytes_read = 0;
 
@@ -170,7 +170,7 @@ static int ssh_stream_write(
 	const char *buffer,
 	size_t len)
 {
-	ssh_stream *s = (ssh_stream *)stream;
+	ssh_stream *s = GIT_CONTAINER_OF(stream, ssh_stream, parent);
 	size_t off = 0;
 	ssize_t ret = 0;
 
@@ -196,7 +196,7 @@ static int ssh_stream_write(
 
 static void ssh_stream_free(git_smart_subtransport_stream *stream)
 {
-	ssh_stream *s = (ssh_stream *)stream;
+	ssh_stream *s = GIT_CONTAINER_OF(stream, ssh_stream, parent);
 	ssh_subtransport *t;
 
 	if (!stream)
@@ -479,7 +479,7 @@ static int _git_ssh_session_create(
 {
 	int rc = 0;
 	LIBSSH2_SESSION* s;
-	git_socket_stream *socket = (git_socket_stream *) io;
+	git_socket_stream *socket = GIT_CONTAINER_OF(io, git_socket_stream, parent);
 
 	assert(session);
 
@@ -730,7 +730,7 @@ static int _ssh_action(
 	const char *url,
 	git_smart_service_t action)
 {
-	ssh_subtransport *t = (ssh_subtransport *) subtransport;
+	ssh_subtransport *t = GIT_CONTAINER_OF(subtransport, ssh_subtransport, parent);
 
 	switch (action) {
 		case GIT_SERVICE_UPLOADPACK_LS:
@@ -752,7 +752,7 @@ static int _ssh_action(
 
 static int _ssh_close(git_smart_subtransport *subtransport)
 {
-	ssh_subtransport *t = (ssh_subtransport *) subtransport;
+	ssh_subtransport *t = GIT_CONTAINER_OF(subtransport, ssh_subtransport, parent);
 
 	assert(!t->current_stream);
 
@@ -763,7 +763,7 @@ static int _ssh_close(git_smart_subtransport *subtransport)
 
 static void _ssh_free(git_smart_subtransport *subtransport)
 {
-	ssh_subtransport *t = (ssh_subtransport *) subtransport;
+	ssh_subtransport *t = GIT_CONTAINER_OF(subtransport, ssh_subtransport, parent);
 
 	assert(!t->current_stream);
 
diff --git a/src/util.h b/src/util.h
index d4fe6ee..b9ab652 100644
--- a/src/util.h
+++ b/src/util.h
@@ -30,6 +30,17 @@
 # define max(a,b) ((a) > (b) ? (a) : (b))
 #endif
 
+#if defined(__GNUC__)
+# define GIT_CONTAINER_OF(ptr, type, member) \
+	__builtin_choose_expr( \
+	    __builtin_offsetof(type, member) == 0 && \
+	    __builtin_types_compatible_p(typeof(&((type *) 0)->member), typeof(ptr)), \
+		((type *) (ptr)), \
+		(void)0)
+#else
+# define GIT_CONTAINER_OF(ptr, type, member) (type *)(ptr)
+#endif
+
 #define GIT_DATE_RFC2822_SZ  32
 
 /**