michael@0: /* michael@0: * Copyright © 2012 Google, Inc. michael@0: * michael@0: * This is part of HarfBuzz, a text shaping library. michael@0: * michael@0: * Permission is hereby granted, without written agreement and without michael@0: * license or royalty fees, to use, copy, modify, and distribute this michael@0: * software and its documentation for any purpose, provided that the michael@0: * above copyright notice and the following two paragraphs appear in michael@0: * all copies of this software. michael@0: * michael@0: * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR michael@0: * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES michael@0: * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN michael@0: * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH michael@0: * DAMAGE. michael@0: * michael@0: * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, michael@0: * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND michael@0: * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS michael@0: * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO michael@0: * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. michael@0: * michael@0: * Google Author(s): Behdad Esfahbod michael@0: */ michael@0: michael@0: #include "hb-private.hh" michael@0: #include "hb-shaper-private.hh" michael@0: #include "hb-atomic-private.hh" michael@0: michael@0: michael@0: static const hb_shaper_pair_t all_shapers[] = { michael@0: #define HB_SHAPER_IMPLEMENT(name) {#name, _hb_##name##_shape}, michael@0: #include "hb-shaper-list.hh" michael@0: #undef HB_SHAPER_IMPLEMENT michael@0: }; michael@0: michael@0: michael@0: /* Thread-safe, lock-free, shapers */ michael@0: michael@0: static const hb_shaper_pair_t *static_shapers; michael@0: michael@0: static inline michael@0: void free_static_shapers (void) michael@0: { michael@0: if (unlikely (static_shapers != all_shapers)) michael@0: free ((void *) static_shapers); michael@0: } michael@0: michael@0: const hb_shaper_pair_t * michael@0: _hb_shapers_get (void) michael@0: { michael@0: retry: michael@0: hb_shaper_pair_t *shapers = (hb_shaper_pair_t *) hb_atomic_ptr_get (&static_shapers); michael@0: michael@0: if (unlikely (!shapers)) michael@0: { michael@0: char *env = getenv ("HB_SHAPER_LIST"); michael@0: if (!env || !*env) { michael@0: (void) hb_atomic_ptr_cmpexch (&static_shapers, NULL, &all_shapers[0]); michael@0: return (const hb_shaper_pair_t *) all_shapers; michael@0: } michael@0: michael@0: /* Not found; allocate one. */ michael@0: shapers = (hb_shaper_pair_t *) malloc (sizeof (all_shapers)); michael@0: if (unlikely (!shapers)) { michael@0: (void) hb_atomic_ptr_cmpexch (&static_shapers, NULL, &all_shapers[0]); michael@0: return (const hb_shaper_pair_t *) all_shapers; michael@0: } michael@0: michael@0: memcpy (shapers, all_shapers, sizeof (all_shapers)); michael@0: michael@0: /* Reorder shaper list to prefer requested shapers. */ michael@0: unsigned int i = 0; michael@0: char *end, *p = env; michael@0: for (;;) { michael@0: end = strchr (p, ','); michael@0: if (!end) michael@0: end = p + strlen (p); michael@0: michael@0: for (unsigned int j = i; j < ARRAY_LENGTH (all_shapers); j++) michael@0: if (end - p == (int) strlen (shapers[j].name) && michael@0: 0 == strncmp (shapers[j].name, p, end - p)) michael@0: { michael@0: /* Reorder this shaper to position i */ michael@0: struct hb_shaper_pair_t t = shapers[j]; michael@0: memmove (&shapers[i + 1], &shapers[i], sizeof (shapers[i]) * (j - i)); michael@0: shapers[i] = t; michael@0: i++; michael@0: } michael@0: michael@0: if (!*end) michael@0: break; michael@0: else michael@0: p = end + 1; michael@0: } michael@0: michael@0: if (!hb_atomic_ptr_cmpexch (&static_shapers, NULL, shapers)) { michael@0: free (shapers); michael@0: goto retry; michael@0: } michael@0: michael@0: #ifdef HAVE_ATEXIT michael@0: atexit (free_static_shapers); /* First person registers atexit() callback. */ michael@0: #endif michael@0: } michael@0: michael@0: return shapers; michael@0: }