/ stdio / xprintf_comp.c
xprintf_comp.c
 1  /*
 2   * Copyright (c) 2012 Apple Inc. All rights reserved.
 3   *
 4   * @APPLE_LICENSE_HEADER_START@
 5   * 
 6   * This file contains Original Code and/or Modifications of Original Code
 7   * as defined in and that are subject to the Apple Public Source License
 8   * Version 2.0 (the 'License'). You may not use this file except in
 9   * compliance with the License. Please obtain a copy of the License at
10   * http://www.opensource.apple.com/apsl/ and read it before using this
11   * file.
12   * 
13   * The Original Code and all software distributed under the License are
14   * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15   * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16   * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17   * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18   * Please see the License for the specific language governing rights and
19   * limitations under the License.
20   * 
21   * @APPLE_LICENSE_HEADER_END@
22   */
23  
24  #include <xlocale_private.h>
25  #include <printf.h>
26  #include <pthread.h>
27  #include <stdlib.h>
28  #include <string.h>
29  #include <errno.h>
30  #include "xprintf_domain.h"
31  #include "xprintf_private.h"
32  
33  #pragma clang diagnostic push
34  #pragma clang diagnostic ignored "-Wpointer-bool-conversion"
35  
36  void
37  free_printf_comp(printf_comp_t pc)
38  {
39      if(!pc) return;
40      XL_RELEASE(pc->loc);
41  #ifdef XPRINTF_PERF
42      arrayfree(pc->pa);
43      free(pc->pa);
44      arrayfree(pc->aa);
45      free(pc->aa);
46      arrayfree(pc->ua);
47      free(pc->ua);
48  #else /* !XPRINTF_PERF */
49      free(pc->pi);
50      free(pc->argt);
51      free(pc->args);
52  #endif /* !XPRINTF_PERF */
53      pthread_mutex_destroy(&pc->mutex);
54      free(pc);
55  }
56  
57  printf_comp_t
58  new_printf_comp(printf_domain_t restrict domain, locale_t loc, const char * restrict fmt)
59  {
60      int ret, saverrno;
61      printf_comp_t restrict pc;
62  
63      if(!domain) {
64  	errno = EINVAL;
65  	return NULL;
66      }
67      pc = MALLOC(sizeof(*pc) + strlen(fmt) + 1);
68      if(!pc) return NULL;
69      bzero(pc, sizeof(*pc));
70      pc->mutex = (pthread_mutex_t)PTHREAD_MUTEX_INITIALIZER;
71      pc->fmt = (const char *)(pc + 1);
72      strcpy((char *)pc->fmt, fmt);
73      DEFAULT_CURRENT_LOCALE(loc);
74      XL_RETAIN(loc);
75      pc->loc = loc;
76      xprintf_domain_init();
77      pthread_rwlock_rdlock(&domain->rwlock);
78      ret = __printf_comp(pc, domain);
79      saverrno = errno;
80      pthread_rwlock_unlock(&domain->rwlock);
81      if(ret < 0) {
82  	XL_RELEASE(loc);
83  	pthread_mutex_destroy(&pc->mutex);
84  	free(pc);
85  	errno = saverrno;
86  	return NULL;
87      }
88      return pc;
89  }
90  #pragma clang diagnostic pop
91