fileext.h revision 17393b06bab9cb3e95d0f466a56c746de19b8eee
11dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/*	$OpenBSD: fileext.h,v 1.2 2005/06/17 20:40:32 espie Exp $	*/
21dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* $NetBSD: fileext.h,v 1.5 2003/07/18 21:46:41 nathanw Exp $ */
31dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
41dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/*-
51dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * Copyright (c)2001 Citrus Project,
61dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * All rights reserved.
71dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
81dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * Redistribution and use in source and binary forms, with or without
91dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * modification, are permitted provided that the following conditions
101dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * are met:
111dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * 1. Redistributions of source code must retain the above copyright
121dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *    notice, this list of conditions and the following disclaimer.
131dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * 2. Redistributions in binary form must reproduce the above copyright
141dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *    notice, this list of conditions and the following disclaimer in the
151dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *    documentation and/or other materials provided with the distribution.
161dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
171dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
181dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
191dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
201dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
211dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
221dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
231dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
241dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
251dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
261dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
271dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * SUCH DAMAGE.
281dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
291dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * $Citrus$
301dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project */
311dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
329287223674afe3957d248ad51200c5abead6ec96Calin Juravle#ifndef _FILEEXT_H_
339287223674afe3957d248ad51200c5abead6ec96Calin Juravle#define _FILEEXT_H_
349287223674afe3957d248ad51200c5abead6ec96Calin Juravle
359831ad3ce6bd5b22da16a275ed67e7236eae3d1fDavid 'Digit' Turner#include <pthread.h>
368c4994bbc1a9a01e34ea92c91eb5b2d1a27bd074Elliott Hughes#include <stdbool.h>
379831ad3ce6bd5b22da16a275ed67e7236eae3d1fDavid 'Digit' Turner
38c48c3e4bb3d1665f3e9fa2785daafa72dfe59399Elliott Hughes__BEGIN_DECLS
39c48c3e4bb3d1665f3e9fa2785daafa72dfe59399Elliott Hughes
401dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/*
411dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * file extension
421dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project */
431dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstruct __sfileext {
448c4994bbc1a9a01e34ea92c91eb5b2d1a27bd074Elliott Hughes	struct	__sbuf _ub;		/* ungetc buffer */
451dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	struct wchar_io_data _wcio;	/* wide char io status */
468c4994bbc1a9a01e34ea92c91eb5b2d1a27bd074Elliott Hughes	pthread_mutex_t _lock;		/* file lock */
478c4994bbc1a9a01e34ea92c91eb5b2d1a27bd074Elliott Hughes	bool _stdio_handles_locking;	/* __fsetlocking support */
481dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project};
491dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
508b5df3920f2843c9cdf04160517c1e8b77c992f5Elliott Hughes#if defined(__cplusplus)
518b5df3920f2843c9cdf04160517c1e8b77c992f5Elliott Hughes#define _EXT(fp) reinterpret_cast<__sfileext*>((fp)->_ext._base)
528b5df3920f2843c9cdf04160517c1e8b77c992f5Elliott Hughes#else
531dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#define _EXT(fp) ((struct __sfileext *)((fp)->_ext._base))
548b5df3920f2843c9cdf04160517c1e8b77c992f5Elliott Hughes#endif
558b5df3920f2843c9cdf04160517c1e8b77c992f5Elliott Hughes
561dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#define _UB(fp) _EXT(fp)->_ub
579831ad3ce6bd5b22da16a275ed67e7236eae3d1fDavid 'Digit' Turner#define _FLOCK(fp)  _EXT(fp)->_lock
581dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
591dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#define _FILEEXT_INIT(fp) \
601dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectdo { \
611dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	_UB(fp)._base = NULL; \
621dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	_UB(fp)._size = 0; \
631dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	WCIO_INIT(fp); \
6417393b06bab9cb3e95d0f466a56c746de19b8eeeYabin Cui	pthread_mutexattr_t attr; \
6517393b06bab9cb3e95d0f466a56c746de19b8eeeYabin Cui	pthread_mutexattr_init(&attr); \
6617393b06bab9cb3e95d0f466a56c746de19b8eeeYabin Cui	pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); \
6717393b06bab9cb3e95d0f466a56c746de19b8eeeYabin Cui	pthread_mutex_init(&_FLOCK(fp), &attr); \
6817393b06bab9cb3e95d0f466a56c746de19b8eeeYabin Cui	pthread_mutexattr_destroy(&attr); \
698c4994bbc1a9a01e34ea92c91eb5b2d1a27bd074Elliott Hughes	_EXT(fp)->_stdio_handles_locking = true; \
701dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project} while (0)
711dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
721dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#define _FILEEXT_SETUP(f, fext) \
731dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectdo { \
741dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	(f)->_ext._base = (unsigned char *)(fext); \
751dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project	_FILEEXT_INIT(f); \
761dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project} while (0)
779287223674afe3957d248ad51200c5abead6ec96Calin Juravle
78c48c3e4bb3d1665f3e9fa2785daafa72dfe59399Elliott Hughes__END_DECLS
79c48c3e4bb3d1665f3e9fa2785daafa72dfe59399Elliott Hughes
809287223674afe3957d248ad51200c5abead6ec96Calin Juravle#endif /* _FILEEXT_H_ */
81