2 * Copyright (C) 2010 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
25 #define INVALID_FD (-1)
28 static struct pooled_fd fdpool_head = {
33 static int fdpool_count = 0;
35 static void fdpool_insert_head(struct pooled_fd *node)
37 struct pooled_fd *prev = &fdpool_head;
38 struct pooled_fd *next = prev->next;
50 static void fdpool_remove(struct pooled_fd *node)
52 struct pooled_fd *prev = node->prev;
53 struct pooled_fd *next = node->next;
64 static struct pooled_fd *fdpool_remove_tail(void)
66 struct pooled_fd *tail = fdpool_head.prev;
68 assert(tail != &fdpool_head);
75 static void fdpool_clear(struct pooled_fd *pfd)
80 pfd->prev = pfd->next = NULL;
83 static void fdpool_unpool(struct pooled_fd *pfd)
89 static void fdpool_evict(void)
91 struct pooled_fd *tail;
93 tail = fdpool_remove_tail();
97 static void fdpool_pool(struct pooled_fd *pfd, int fd)
99 if (fdpool_count >= FDPOOL_SIZE)
102 fdpool_insert_head(pfd);
106 static void fdpool_touch(struct pooled_fd *pfd)
109 fdpool_insert_head(pfd);
114 void fdpool_init(struct pooled_fd *pfd)
119 int fdpool_open(struct pooled_fd *pfd, const char *pathname, int flags)
124 if (pfd->fd != INVALID_FD) {
129 fd = open(pathname, flags);
133 fdpool_pool(pfd, fd);
140 void fdpool_close(struct pooled_fd *pfd)