summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Opalach <andrew@akon.city> 2023-10-07 17:12:38 -0400
committerAndrew Opalach <andrew@akon.city> 2023-10-07 17:44:10 -0400
commitb07771669ae029d07b76a58ed526da7bc29ec27b (patch)
treea379a6b638a64060de1c220537f803b6402ffdea
downloadlibalabaster-b07771669ae029d07b76a58ed526da7bc29ec27b.tar.gz
libalabaster-b07771669ae029d07b76a58ed526da7bc29ec27b.tar.bz2
libalabaster-b07771669ae029d07b76a58ed526da7bc29ec27b.zip
Add libalabaster
Signed-off-by: Andrew Opalach <andrew@akon.city>
-rw-r--r--.gitignore5
-rw-r--r--LICENSE674
-rw-r--r--NOTES.md12
-rw-r--r--README.md1
-rw-r--r--include/al/array.h11
-rw-r--r--include/al/array_sort.h19
-rw-r--r--include/al/array_typed.h151
-rw-r--r--include/al/atomic.h42
-rw-r--r--include/al/lib.h157
-rw-r--r--include/al/list.h132
-rw-r--r--include/al/log.h31
-rw-r--r--include/al/random.h28
-rw-r--r--include/al/ring_buffer.h31
-rw-r--r--include/al/str.h161
-rw-r--r--include/al/test.h142
-rw-r--r--include/al/types.h61
-rw-r--r--include/al/wstr.h81
-rw-r--r--meson.build52
-rw-r--r--meson_options.txt1
-rw-r--r--src/array.c105
-rw-r--r--src/lib.c184
-rw-r--r--src/log.c90
-rw-r--r--src/ring_buffer.c169
-rw-r--r--src/str.c113
-rw-r--r--subprojects/c89atomic.wrap5
-rw-r--r--subprojects/packagefiles/c89atomic/meson.build2
-rw-r--r--tests/array.c256
-rw-r--r--tests/array.h8
-rw-r--r--tests/array_typed.c261
-rw-r--r--tests/array_typed.h8
-rw-r--r--tests/main.c23
-rw-r--r--tests/ring_buffer.c198
-rw-r--r--tests/ring_buffer.h8
-rw-r--r--tests/str.c191
-rw-r--r--tests/str.h8
35 files changed, 3421 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..47e95ff
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,5 @@
+build*/
+compile_commands.json
+.cache/
+.clangd/
+subprojects/c89atomic/
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..f288702
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,674 @@
+ GNU GENERAL PUBLIC LICENSE
+ Version 3, 29 June 2007
+
+ Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
+ Everyone is permitted to copy and distribute verbatim copies
+ of this license document, but changing it is not allowed.
+
+ Preamble
+
+ The GNU General Public License is a free, copyleft license for
+software and other kinds of works.
+
+ The licenses for most software and other practical works are designed
+to take away your freedom to share and change the works. By contrast,
+the GNU General Public License is intended to guarantee your freedom to
+share and change all versions of a program--to make sure it remains free
+software for all its users. We, the Free Software Foundation, use the
+GNU General Public License for most of our software; it applies also to
+any other work released this way by its authors. You can apply it to
+your programs, too.
+
+ When we speak of free software, we are referring to freedom, not
+price. Our General Public Licenses are designed to make sure that you
+have the freedom to distribute copies of free software (and charge for
+them if you wish), that you receive source code or can get it if you
+want it, that you can change the software or use pieces of it in new
+free programs, and that you know you can do these things.
+
+ To protect your rights, we need to prevent others from denying you
+these rights or asking you to surrender the rights. Therefore, you have
+certain responsibilities if you distribute copies of the software, or if
+you modify it: responsibilities to respect the freedom of others.
+
+ For example, if you distribute copies of such a program, whether
+gratis or for a fee, you must pass on to the recipients the same
+freedoms that you received. You must make sure that they, too, receive
+or can get the source code. And you must show them these terms so they
+know their rights.
+
+ Developers that use the GNU GPL protect your rights with two steps:
+(1) assert copyright on the software, and (2) offer you this License
+giving you legal permission to copy, distribute and/or modify it.
+
+ For the developers' and authors' protection, the GPL clearly explains
+that there is no warranty for this free software. For both users' and
+authors' sake, the GPL requires that modified versions be marked as
+changed, so that their problems will not be attributed erroneously to
+authors of previous versions.
+
+ Some devices are designed to deny users access to install or run
+modified versions of the software inside them, although the manufacturer
+can do so. This is fundamentally incompatible with the aim of
+protecting users' freedom to change the software. The systematic
+pattern of such abuse occurs in the area of products for individuals to
+use, which is precisely where it is most unacceptable. Therefore, we
+have designed this version of the GPL to prohibit the practice for those
+products. If such problems arise substantially in other domains, we
+stand ready to extend this provision to those domains in future versions
+of the GPL, as needed to protect the freedom of users.
+
+ Finally, every program is threatened constantly by software patents.
+States should not allow patents to restrict development and use of
+software on general-purpose computers, but in those that do, we wish to
+avoid the special danger that patents applied to a free program could
+make it effectively proprietary. To prevent this, the GPL assures that
+patents cannot be used to render the program non-free.
+
+ The precise terms and conditions for copying, distribution and
+modification follow.
+
+ TERMS AND CONDITIONS
+
+ 0. Definitions.
+
+ "This License" refers to version 3 of the GNU General Public License.
+
+ "Copyright" also means copyright-like laws that apply to other kinds of
+works, such as semiconductor masks.
+
+ "The Program" refers to any copyrightable work licensed under this
+License. Each licensee is addressed as "you". "Licensees" and
+"recipients" may be individuals or organizations.
+
+ To "modify" a work means to copy from or adapt all or part of the work
+in a fashion requiring copyright permission, other than the making of an
+exact copy. The resulting work is called a "modified version" of the
+earlier work or a work "based on" the earlier work.
+
+ A "covered work" means either the unmodified Program or a work based
+on the Program.
+
+ To "propagate" a work means to do anything with it that, without
+permission, would make you directly or secondarily liable for
+infringement under applicable copyright law, except executing it on a
+computer or modifying a private copy. Propagation includes copying,
+distribution (with or without modification), making available to the
+public, and in some countries other activities as well.
+
+ To "convey" a work means any kind of propagation that enables other
+parties to make or receive copies. Mere interaction with a user through
+a computer network, with no transfer of a copy, is not conveying.
+
+ An interactive user interface displays "Appropriate Legal Notices"
+to the extent that it includes a convenient and prominently visible
+feature that (1) displays an appropriate copyright notice, and (2)
+tells the user that there is no warranty for the work (except to the
+extent that warranties are provided), that licensees may convey the
+work under this License, and how to view a copy of this License. If
+the interface presents a list of user commands or options, such as a
+menu, a prominent item in the list meets this criterion.
+
+ 1. Source Code.
+
+ The "source code" for a work means the preferred form of the work
+for making modifications to it. "Object code" means any non-source
+form of a work.
+
+ A "Standard Interface" means an interface that either is an official
+standard defined by a recognized standards body, or, in the case of
+interfaces specified for a particular programming language, one that
+is widely used among developers working in that language.
+
+ The "System Libraries" of an executable work include anything, other
+than the work as a whole, that (a) is included in the normal form of
+packaging a Major Component, but which is not part of that Major
+Component, and (b) serves only to enable use of the work with that
+Major Component, or to implement a Standard Interface for which an
+implementation is available to the public in source code form. A
+"Major Component", in this context, means a major essential component
+(kernel, window system, and so on) of the specific operating system
+(if any) on which the executable work runs, or a compiler used to
+produce the work, or an object code interpreter used to run it.
+
+ The "Corresponding Source" for a work in object code form means all
+the source code needed to generate, install, and (for an executable
+work) run the object code and to modify the work, including scripts to
+control those activities. However, it does not include the work's
+System Libraries, or general-purpose tools or generally available free
+programs which are used unmodified in performing those activities but
+which are not part of the work. For example, Corresponding Source
+includes interface definition files associated with source files for
+the work, and the source code for shared libraries and dynamically
+linked subprograms that the work is specifically designed to require,
+such as by intimate data communication or control flow between those
+subprograms and other parts of the work.
+
+ The Corresponding Source need not include anything that users
+can regenerate automatically from other parts of the Corresponding
+Source.
+
+ The Corresponding Source for a work in source code form is that
+same work.
+
+ 2. Basic Permissions.
+
+ All rights granted under this License are granted for the term of
+copyright on the Program, and are irrevocable provided the stated
+conditions are met. This License explicitly affirms your unlimited
+permission to run the unmodified Program. The output from running a
+covered work is covered by this License only if the output, given its
+content, constitutes a covered work. This License acknowledges your
+rights of fair use or other equivalent, as provided by copyright law.
+
+ You may make, run and propagate covered works that you do not
+convey, without conditions so long as your license otherwise remains
+in force. You may convey covered works to others for the sole purpose
+of having them make modifications exclusively for you, or provide you
+with facilities for running those works, provided that you comply with
+the terms of this License in conveying all material for which you do
+not control copyright. Those thus making or running the covered works
+for you must do so exclusively on your behalf, under your direction
+and control, on terms that prohibit them from making any copies of
+your copyrighted material outside their relationship with you.
+
+ Conveying under any other circumstances is permitted solely under
+the conditions stated below. Sublicensing is not allowed; section 10
+makes it unnecessary.
+
+ 3. Protecting Users' Legal Rights From Anti-Circumvention Law.
+
+ No covered work shall be deemed part of an effective technological
+measure under any applicable law fulfilling obligations under article
+11 of the WIPO copyright treaty adopted on 20 December 1996, or
+similar laws prohibiting or restricting circumvention of such
+measures.
+
+ When you convey a covered work, you waive any legal power to forbid
+circumvention of technological measures to the extent such circumvention
+is effected by exercising rights under this License with respect to
+the covered work, and you disclaim any intention to limit operation or
+modification of the work as a means of enforcing, against the work's
+users, your or third parties' legal rights to forbid circumvention of
+technological measures.
+
+ 4. Conveying Verbatim Copies.
+
+ You may convey verbatim copies of the Program's source code as you
+receive it, in any medium, provided that you conspicuously and
+appropriately publish on each copy an appropriate copyright notice;
+keep intact all notices stating that this License and any
+non-permissive terms added in accord with section 7 apply to the code;
+keep intact all notices of the absence of any warranty; and give all
+recipients a copy of this License along with the Program.
+
+ You may charge any price or no price for each copy that you convey,
+and you may offer support or warranty protection for a fee.
+
+ 5. Conveying Modified Source Versions.
+
+ You may convey a work based on the Program, or the modifications to
+produce it from the Program, in the form of source code under the
+terms of section 4, provided that you also meet all of these conditions:
+
+ a) The work must carry prominent notices stating that you modified
+ it, and giving a relevant date.
+
+ b) The work must carry prominent notices stating that it is
+ released under this License and any conditions added under section
+ 7. This requirement modifies the requirement in section 4 to
+ "keep intact all notices".
+
+ c) You must license the entire work, as a whole, under this
+ License to anyone who comes into possession of a copy. This
+ License will therefore apply, along with any applicable section 7
+ additional terms, to the whole of the work, and all its parts,
+ regardless of how they are packaged. This License gives no
+ permission to license the work in any other way, but it does not
+ invalidate such permission if you have separately received it.
+
+ d) If the work has interactive user interfaces, each must display
+ Appropriate Legal Notices; however, if the Program has interactive
+ interfaces that do not display Appropriate Legal Notices, your
+ work need not make them do so.
+
+ A compilation of a covered work with other separate and independent
+works, which are not by their nature extensions of the covered work,
+and which are not combined with it such as to form a larger program,
+in or on a volume of a storage or distribution medium, is called an
+"aggregate" if the compilation and its resulting copyright are not
+used to limit the access or legal rights of the compilation's users
+beyond what the individual works permit. Inclusion of a covered work
+in an aggregate does not cause this License to apply to the other
+parts of the aggregate.
+
+ 6. Conveying Non-Source Forms.
+
+ You may convey a covered work in object code form under the terms
+of sections 4 and 5, provided that you also convey the
+machine-readable Corresponding Source under the terms of this License,
+in one of these ways:
+
+ a) Convey the object code in, or embodied in, a physical product
+ (including a physical distribution medium), accompanied by the
+ Corresponding Source fixed on a durable physical medium
+ customarily used for software interchange.
+
+ b) Convey the object code in, or embodied in, a physical product
+ (including a physical distribution medium), accompanied by a
+ written offer, valid for at least three years and valid for as
+ long as you offer spare parts or customer support for that product
+ model, to give anyone who possesses the object code either (1) a
+ copy of the Corresponding Source for all the software in the
+ product that is covered by this License, on a durable physical
+ medium customarily used for software interchange, for a price no
+ more than your reasonable cost of physically performing this
+ conveying of source, or (2) access to copy the
+ Corresponding Source from a network server at no charge.
+
+ c) Convey individual copies of the object code with a copy of the
+ written offer to provide the Corresponding Source. This
+ alternative is allowed only occasionally and noncommercially, and
+ only if you received the object code with such an offer, in accord
+ with subsection 6b.
+
+ d) Convey the object code by offering access from a designated
+ place (gratis or for a charge), and offer equivalent access to the
+ Corresponding Source in the same way through the same place at no
+ further charge. You need not require recipients to copy the
+ Corresponding Source along with the object code. If the place to
+ copy the object code is a network server, the Corresponding Source
+ may be on a different server (operated by you or a third party)
+ that supports equivalent copying facilities, provided you maintain
+ clear directions next to the object code saying where to find the
+ Corresponding Source. Regardless of what server hosts the
+ Corresponding Source, you remain obligated to ensure that it is
+ available for as long as needed to satisfy these requirements.
+
+ e) Convey the object code using peer-to-peer transmission, provided
+ you inform other peers where the object code and Corresponding
+ Source of the work are being offered to the general public at no
+ charge under subsection 6d.
+
+ A separable portion of the object code, whose source code is excluded
+from the Corresponding Source as a System Library, need not be
+included in conveying the object code work.
+
+ A "User Product" is either (1) a "consumer product", which means any
+tangible personal property which is normally used for personal, family,
+or household purposes, or (2) anything designed or sold for incorporation
+into a dwelling. In determining whether a product is a consumer product,
+doubtful cases shall be resolved in favor of coverage. For a particular
+product received by a particular user, "normally used" refers to a
+typical or common use of that class of product, regardless of the status
+of the particular user or of the way in which the particular user
+actually uses, or expects or is expected to use, the product. A product
+is a consumer product regardless of whether the product has substantial
+commercial, industrial or non-consumer uses, unless such uses represent
+the only significant mode of use of the product.
+
+ "Installation Information" for a User Product means any methods,
+procedures, authorization keys, or other information required to install
+and execute modified versions of a covered work in that User Product from
+a modified version of its Corresponding Source. The information must
+suffice to ensure that the continued functioning of the modified object
+code is in no case prevented or interfered with solely because
+modification has been made.
+
+ If you convey an object code work under this section in, or with, or
+specifically for use in, a User Product, and the conveying occurs as
+part of a transaction in which the right of possession and use of the
+User Product is transferred to the recipient in perpetuity or for a
+fixed term (regardless of how the transaction is characterized), the
+Corresponding Source conveyed under this section must be accompanied
+by the Installation Information. But this requirement does not apply
+if neither you nor any third party retains the ability to install
+modified object code on the User Product (for example, the work has
+been installed in ROM).
+
+ The requirement to provide Installation Information does not include a
+requirement to continue to provide support service, warranty, or updates
+for a work that has been modified or installed by the recipient, or for
+the User Product in which it has been modified or installed. Access to a
+network may be denied when the modification itself materially and
+adversely affects the operation of the network or violates the rules and
+protocols for communication across the network.
+
+ Corresponding Source conveyed, and Installation Information provided,
+in accord with this section must be in a format that is publicly
+documented (and with an implementation available to the public in
+source code form), and must require no special password or key for
+unpacking, reading or copying.
+
+ 7. Additional Terms.
+
+ "Additional permissions" are terms that supplement the terms of this
+License by making exceptions from one or more of its conditions.
+Additional permissions that are applicable to the entire Program shall
+be treated as though they were included in this License, to the extent
+that they are valid under applicable law. If additional permissions
+apply only to part of the Program, that part may be used separately
+under those permissions, but the entire Program remains governed by
+this License without regard to the additional permissions.
+
+ When you convey a copy of a covered work, you may at your option
+remove any additional permissions from that copy, or from any part of
+it. (Additional permissions may be written to require their own
+removal in certain cases when you modify the work.) You may place
+additional permissions on material, added by you to a covered work,
+for which you have or can give appropriate copyright permission.
+
+ Notwithstanding any other provision of this License, for material you
+add to a covered work, you may (if authorized by the copyright holders of
+that material) supplement the terms of this License with terms:
+
+ a) Disclaiming warranty or limiting liability differently from the
+ terms of sections 15 and 16 of this License; or
+
+ b) Requiring preservation of specified reasonable legal notices or
+ author attributions in that material or in the Appropriate Legal
+ Notices displayed by works containing it; or
+
+ c) Prohibiting misrepresentation of the origin of that material, or
+ requiring that modified versions of such material be marked in
+ reasonable ways as different from the original version; or
+
+ d) Limiting the use for publicity purposes of names of licensors or
+ authors of the material; or
+
+ e) Declining to grant rights under trademark law for use of some
+ trade names, trademarks, or service marks; or
+
+ f) Requiring indemnification of licensors and authors of that
+ material by anyone who conveys the material (or modified versions of
+ it) with contractual assumptions of liability to the recipient, for
+ any liability that these contractual assumptions directly impose on
+ those licensors and authors.
+
+ All other non-permissive additional terms are considered "further
+restrictions" within the meaning of section 10. If the Program as you
+received it, or any part of it, contains a notice stating that it is
+governed by this License along with a term that is a further
+restriction, you may remove that term. If a license document contains
+a further restriction but permits relicensing or conveying under this
+License, you may add to a covered work material governed by the terms
+of that license document, provided that the further restriction does
+not survive such relicensing or conveying.
+
+ If you add terms to a covered work in accord with this section, you
+must place, in the relevant source files, a statement of the
+additional terms that apply to those files, or a notice indicating
+where to find the applicable terms.
+
+ Additional terms, permissive or non-permissive, may be stated in the
+form of a separately written license, or stated as exceptions;
+the above requirements apply either way.
+
+ 8. Termination.
+
+ You may not propagate or modify a covered work except as expressly
+provided under this License. Any attempt otherwise to propagate or
+modify it is void, and will automatically terminate your rights under
+this License (including any patent licenses granted under the third
+paragraph of section 11).
+
+ However, if you cease all violation of this License, then your
+license from a particular copyright holder is reinstated (a)
+provisionally, unless and until the copyright holder explicitly and
+finally terminates your license, and (b) permanently, if the copyright
+holder fails to notify you of the violation by some reasonable means
+prior to 60 days after the cessation.
+
+ Moreover, your license from a particular copyright holder is
+reinstated permanently if the copyright holder notifies you of the
+violation by some reasonable means, this is the first time you have
+received notice of violation of this License (for any work) from that
+copyright holder, and you cure the violation prior to 30 days after
+your receipt of the notice.
+
+ Termination of your rights under this section does not terminate the
+licenses of parties who have received copies or rights from you under
+this License. If your rights have been terminated and not permanently
+reinstated, you do not qualify to receive new licenses for the same
+material under section 10.
+
+ 9. Acceptance Not Required for Having Copies.
+
+ You are not required to accept this License in order to receive or
+run a copy of the Program. Ancillary propagation of a covered work
+occurring solely as a consequence of using peer-to-peer transmission
+to receive a copy likewise does not require acceptance. However,
+nothing other than this License grants you permission to propagate or
+modify any covered work. These actions infringe copyright if you do
+not accept this License. Therefore, by modifying or propagating a
+covered work, you indicate your acceptance of this License to do so.
+
+ 10. Automatic Licensing of Downstream Recipients.
+
+ Each time you convey a covered work, the recipient automatically
+receives a license from the original licensors, to run, modify and
+propagate that work, subject to this License. You are not responsible
+for enforcing compliance by third parties with this License.
+
+ An "entity transaction" is a transaction transferring control of an
+organization, or substantially all assets of one, or subdividing an
+organization, or merging organizations. If propagation of a covered
+work results from an entity transaction, each party to that
+transaction who receives a copy of the work also receives whatever
+licenses to the work the party's predecessor in interest had or could
+give under the previous paragraph, plus a right to possession of the
+Corresponding Source of the work from the predecessor in interest, if
+the predecessor has it or can get it with reasonable efforts.
+
+ You may not impose any further restrictions on the exercise of the
+rights granted or affirmed under this License. For example, you may
+not impose a license fee, royalty, or other charge for exercise of
+rights granted under this License, and you may not initiate litigation
+(including a cross-claim or counterclaim in a lawsuit) alleging that
+any patent claim is infringed by making, using, selling, offering for
+sale, or importing the Program or any portion of it.
+
+ 11. Patents.
+
+ A "contributor" is a copyright holder who authorizes use under this
+License of the Program or a work on which the Program is based. The
+work thus licensed is called the contributor's "contributor version".
+
+ A contributor's "essential patent claims" are all patent claims
+owned or controlled by the contributor, whether already acquired or
+hereafter acquired, that would be infringed by some manner, permitted
+by this License, of making, using, or selling its contributor version,
+but do not include claims that would be infringed only as a
+consequence of further modification of the contributor version. For
+purposes of this definition, "control" includes the right to grant
+patent sublicenses in a manner consistent with the requirements of
+this License.
+
+ Each contributor grants you a non-exclusive, worldwide, royalty-free
+patent license under the contributor's essential patent claims, to
+make, use, sell, offer for sale, import and otherwise run, modify and
+propagate the contents of its contributor version.
+
+ In the following three paragraphs, a "patent license" is any express
+agreement or commitment, however denominated, not to enforce a patent
+(such as an express permission to practice a patent or covenant not to
+sue for patent infringement). To "grant" such a patent license to a
+party means to make such an agreement or commitment not to enforce a
+patent against the party.
+
+ If you convey a covered work, knowingly relying on a patent license,
+and the Corresponding Source of the work is not available for anyone
+to copy, free of charge and under the terms of this License, through a
+publicly available network server or other readily accessible means,
+then you must either (1) cause the Corresponding Source to be so
+available, or (2) arrange to deprive yourself of the benefit of the
+patent license for this particular work, or (3) arrange, in a manner
+consistent with the requirements of this License, to extend the patent
+license to downstream recipients. "Knowingly relying" means you have
+actual knowledge that, but for the patent license, your conveying the
+covered work in a country, or your recipient's use of the covered work
+in a country, would infringe one or more identifiable patents in that
+country that you have reason to believe are valid.
+
+ If, pursuant to or in connection with a single transaction or
+arrangement, you convey, or propagate by procuring conveyance of, a
+covered work, and grant a patent license to some of the parties
+receiving the covered work authorizing them to use, propagate, modify
+or convey a specific copy of the covered work, then the patent license
+you grant is automatically extended to all recipients of the covered
+work and works based on it.
+
+ A patent license is "discriminatory" if it does not include within
+the scope of its coverage, prohibits the exercise of, or is
+conditioned on the non-exercise of one or more of the rights that are
+specifically granted under this License. You may not convey a covered
+work if you are a party to an arrangement with a third party that is
+in the business of distributing software, under which you make payment
+to the third party based on the extent of your activity of conveying
+the work, and under which the third party grants, to any of the
+parties who would receive the covered work from you, a discriminatory
+patent license (a) in connection with copies of the covered work
+conveyed by you (or copies made from those copies), or (b) primarily
+for and in connection with specific products or compilations that
+contain the covered work, unless you entered into that arrangement,
+or that patent license was granted, prior to 28 March 2007.
+
+ Nothing in this License shall be construed as excluding or limiting
+any implied license or other defenses to infringement that may
+otherwise be available to you under applicable patent law.
+
+ 12. No Surrender of Others' Freedom.
+
+ If conditions are imposed on you (whether by court order, agreement or
+otherwise) that contradict the conditions of this License, they do not
+excuse you from the conditions of this License. If you cannot convey a
+covered work so as to satisfy simultaneously your obligations under this
+License and any other pertinent obligations, then as a consequence you may
+not convey it at all. For example, if you agree to terms that obligate you
+to collect a royalty for further conveying from those to whom you convey
+the Program, the only way you could satisfy both those terms and this
+License would be to refrain entirely from conveying the Program.
+
+ 13. Use with the GNU Affero General Public License.
+
+ Notwithstanding any other provision of this License, you have
+permission to link or combine any covered work with a work licensed
+under version 3 of the GNU Affero General Public License into a single
+combined work, and to convey the resulting work. The terms of this
+License will continue to apply to the part which is the covered work,
+but the special requirements of the GNU Affero General Public License,
+section 13, concerning interaction through a network will apply to the
+combination as such.
+
+ 14. Revised Versions of this License.
+
+ The Free Software Foundation may publish revised and/or new versions of
+the GNU General Public License from time to time. Such new versions will
+be similar in spirit to the present version, but may differ in detail to
+address new problems or concerns.
+
+ Each version is given a distinguishing version number. If the
+Program specifies that a certain numbered version of the GNU General
+Public License "or any later version" applies to it, you have the
+option of following the terms and conditions either of that numbered
+version or of any later version published by the Free Software
+Foundation. If the Program does not specify a version number of the
+GNU General Public License, you may choose any version ever published
+by the Free Software Foundation.
+
+ If the Program specifies that a proxy can decide which future
+versions of the GNU General Public License can be used, that proxy's
+public statement of acceptance of a version permanently authorizes you
+to choose that version for the Program.
+
+ Later license versions may give you additional or different
+permissions. However, no additional obligations are imposed on any
+author or copyright holder as a result of your choosing to follow a
+later version.
+
+ 15. Disclaimer of Warranty.
+
+ THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
+APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
+HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
+OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
+THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
+IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
+ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
+
+ 16. Limitation of Liability.
+
+ IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
+WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
+THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
+GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
+USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
+DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
+PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
+EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
+SUCH DAMAGES.
+
+ 17. Interpretation of Sections 15 and 16.
+
+ If the disclaimer of warranty and limitation of liability provided
+above cannot be given local legal effect according to their terms,
+reviewing courts shall apply local law that most closely approximates
+an absolute waiver of all civil liability in connection with the
+Program, unless a warranty or assumption of liability accompanies a
+copy of the Program in return for a fee.
+
+ END OF TERMS AND CONDITIONS
+
+ How to Apply These Terms to Your New Programs
+
+ If you develop a new program, and you want it to be of the greatest
+possible use to the public, the best way to achieve this is to make it
+free software which everyone can redistribute and change under these terms.
+
+ To do so, attach the following notices to the program. It is safest
+to attach them to the start of each source file to most effectively
+state the exclusion of warranty; and each file should have at least
+the "copyright" line and a pointer to where the full notice is found.
+
+ <one line to give the program's name and a brief idea of what it does.>
+ Copyright (C) <year> <name of author>
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+Also add information on how to contact you by electronic and paper mail.
+
+ If the program does terminal interaction, make it output a short
+notice like this when it starts in an interactive mode:
+
+ <program> Copyright (C) <year> <name of author>
+ This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
+ This is free software, and you are welcome to redistribute it
+ under certain conditions; type `show c' for details.
+
+The hypothetical commands `show w' and `show c' should show the appropriate
+parts of the General Public License. Of course, your program's commands
+might be different; for a GUI interface, you would use an "about box".
+
+ You should also get your employer (if you work as a programmer) or school,
+if any, to sign a "copyright disclaimer" for the program, if necessary.
+For more information on this, and how to apply and follow the GNU GPL, see
+<https://www.gnu.org/licenses/>.
+
+ The GNU General Public License does not permit incorporating your program
+into proprietary programs. If your program is a subroutine library, you
+may consider it more useful to permit linking proprietary applications with
+the library. If this is what you want to do, use the GNU Lesser General
+Public License instead of this License. But first, please read
+<https://www.gnu.org/licenses/why-not-lgpl.html>.
diff --git a/NOTES.md b/NOTES.md
new file mode 100644
index 0000000..7e1fc15
--- /dev/null
+++ b/NOTES.md
@@ -0,0 +1,12 @@
+# Component Ideas
+
+## al_hash_map
+ - swiss tables?
+ - https://abseil.io/blog/20180927-swisstables
+ - https://abseil.io/about/design/swisstables
+
+## al_str_builder
+### Example (pl_str_builder)
+ - https://github.com/haasn/libplacebo/commit/aaf16a05c39f36d96896a9a3a05051749a587e6d
+ - Lazily builds string based on "templates" which are functions that describe how
+ data should get laid out in the final buffer.
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..0d081b9
--- /dev/null
+++ b/README.md
@@ -0,0 +1 @@
+# libalabaster: C Foundation Library
diff --git a/include/al/array.h b/include/al/array.h
new file mode 100644
index 0000000..07fd794
--- /dev/null
+++ b/include/al/array.h
@@ -0,0 +1,11 @@
+#ifndef _AL_ARRAY_H
+#define _AL_ARRAY_H
+
+#include "lib.h"
+
+#define AL_ARRAY_MALLOC al_malloc
+#define AL_ARRAY_REALLOC al_realloc
+#define AL_ARRAY_FREE al_free
+#include "../../src/array.c"
+
+#endif //_AL_ARRAY_H
diff --git a/include/al/array_sort.h b/include/al/array_sort.h
new file mode 100644
index 0000000..51011f4
--- /dev/null
+++ b/include/al/array_sort.h
@@ -0,0 +1,19 @@
+#ifndef _AL_ARRAY_SORT_H
+#define _AL_ARRAY_SORT_H
+
+#define AL_INSERSION_SORT(data, type, count, cmp) \
+ do { \
+ s32 i = 1; \
+ while (i < (s32)count) { \
+ s32 j = i - 1; \
+ type x = data[i]; \
+ while (j >= 0 && cmp(&data[j], &x) > 0) { \
+ data[j + 1] = data[j]; \
+ j--; \
+ } \
+ data[j + 1] = x; \
+ i++; \
+ } \
+ } while (0);
+
+#endif // _AL_ARRAY_SORT_H
diff --git a/include/al/array_typed.h b/include/al/array_typed.h
new file mode 100644
index 0000000..678aef4
--- /dev/null
+++ b/include/al/array_typed.h
@@ -0,0 +1,151 @@
+#ifndef _AL_ARRAY_TYPED_H
+#define _AL_ARRAY_TYPED_H
+
+#ifndef HAVE_GNU_EXTENSIONS
+#error "missing required GNU C extensions"
+#endif
+
+#define _array(N, ...) AL_PASTER(array, AL_PASTER(N, __VA_ARGS__))
+#define _al_array_init(N, ...) AL_PASTER(_array_init, AL_PASTER(N, __VA_ARGS__))
+#define _al_array_data(N, ...) AL_PASTER(_array_data, AL_PASTER(N, __VA_ARGS__))
+#define _al_array_reserve(N, ...) AL_PASTER(_array_reserve, AL_PASTER(N, __VA_ARGS__))
+#define __al_array_reserve_internal(N, ...) AL_PASTER(_array_reserve_internal, AL_PASTER(N, __VA_ARGS__))
+#define _al_array_at(N, ...) AL_PASTER(_array_at, AL_PASTER(N, __VA_ARGS__))
+#define _al_array_last(N, ...) AL_PASTER(_array_last, AL_PASTER(N, __VA_ARGS__))
+#define _al_array_push(N, ...) AL_PASTER(_array_push, AL_PASTER(N, __VA_ARGS__))
+#define _al_array_pop(N, ...) AL_PASTER(_array_pop, AL_PASTER(N, __VA_ARGS__))
+#define _al_array_remove_at(N, ...) AL_PASTER(_array_remove_at, AL_PASTER(N, __VA_ARGS__))
+#define _al_array_pop_at(N, ...) AL_PASTER(_array_pop_at, AL_PASTER(N, __VA_ARGS__))
+#define _al_array_insert(N, ...) AL_PASTER(_array_insert, AL_PASTER(N, __VA_ARGS__))
+#define _al_array_free(N, ...) AL_PASTER(_array_free, AL_PASTER(N, __VA_ARGS__))
+
+#define array(...) AL_EMPTY_DEFAULT(_array, __VA_ARGS__)
+#define al_array_init(...) AL_EMPTY_DEFAULT(_al_array_init, __VA_ARGS__)
+#define al_array_data(...) AL_EMPTY_DEFAULT(_al_array_data, __VA_ARGS__)
+#define _al_array_reserve_internal(...) AL_EMPTY_DEFAULT(__al_array_reserve_internal, __VA_ARGS__)
+#define al_array_reserve(...) AL_EMPTY_DEFAULT(_al_array_reserve, __VA_ARGS__)
+#define al_array_at(...) AL_EMPTY_DEFAULT(_al_array_at, __VA_ARGS__)
+#define al_array_last(...) AL_EMPTY_DEFAULT(_al_array_last, __VA_ARGS__)
+#define al_array_push(...) AL_EMPTY_DEFAULT(_al_array_push, __VA_ARGS__)
+#define al_array_pop(...) AL_EMPTY_DEFAULT(_al_array_pop, __VA_ARGS__)
+#define al_array_remove_at(...) AL_EMPTY_DEFAULT(_al_array_remove_at, __VA_ARGS__)
+#define al_array_pop_at(...) AL_EMPTY_DEFAULT(_al_array_pop_at, __VA_ARGS__)
+#define al_array_insert(...) AL_EMPTY_DEFAULT(_al_array_insert, __VA_ARGS__)
+#define al_array_free(...) AL_EMPTY_DEFAULT(_al_array_free, __VA_ARGS__)
+
+#define AL_ARRAY_DEFINE_FUNCTIONS(N, T, ...) \
+ static inline T *al_array_at(N, __VA_ARGS__)(array(N, __VA_ARGS__) *a, u32 i) \
+ { \
+ return &al_array_data(N, __VA_ARGS__)(a)[i]; \
+ } \
+ static inline T *al_array_last(N, __VA_ARGS__)(array(N, __VA_ARGS__) *a) \
+ { \
+ return al_array_at(N, __VA_ARGS__)(a, a->size - 1); \
+ } \
+ static void al_array_reserve(N, __VA_ARGS__)(array(N, __VA_ARGS__) *a, u32 size); \
+ static inline void al_array_push(N, __VA_ARGS__)(array(N, __VA_ARGS__) *a, T *value) \
+ { \
+ al_array_reserve(N, __VA_ARGS__)(a, a->size + 1); \
+ *al_array_at(N, __VA_ARGS__)(a, a->size++) = *value; \
+ } \
+ static inline void al_array_pop(N, __VA_ARGS__)(array(N, __VA_ARGS__) *a, T *ret) \
+ { \
+ *ret = *al_array_at(N, __VA_ARGS__)(a, --a->size); \
+ } \
+ static inline void al_array_remove_at(N, __VA_ARGS__)(array(N, __VA_ARGS__) *a, u32 i) \
+ { \
+ if (UNLIKELY(i == a->size - 1)) { \
+ a->size--; \
+ } else { \
+ al_memmove(al_array_at(N, __VA_ARGS__)(a, i), al_array_at(N, __VA_ARGS__)(a, i + 1), \
+ sizeof(T) * (--a->size - i)); \
+ } \
+ } \
+ static inline void al_array_pop_at(N, __VA_ARGS__)(array(N, __VA_ARGS__) *a, u32 i, T *ret) \
+ { \
+ *ret = *al_array_at(N, __VA_ARGS__)(a, i); \
+ al_array_remove_at(N, __VA_ARGS__)(a, i); \
+ } \
+ static inline void al_array_insert(N, __VA_ARGS__)(array(N, __VA_ARGS__) *a, T *item, u32 i) \
+ { \
+ if (a->size > i) { \
+ al_array_reserve(N, __VA_ARGS__)(a, a->size + 1); \
+ al_memmove(al_array_at(N, __VA_ARGS__)(a, i + 1), al_array_at(N, __VA_ARGS__)(a, i), \
+ sizeof(T) * (a->size - i)); \
+ a->size++; \
+ } else { \
+ al_array_reserve(N, __VA_ARGS__)(a, i + 1); \
+ a->size = i + 1; \
+ } \
+ *al_array_at(N, __VA_ARGS__)(a, i) = *item; \
+ } \
+ static inline void _al_array_reserve_internal(N, __VA_ARGS__)(array(N, __VA_ARGS__) *a, u32 size) \
+ { \
+ if (size < al_grow_limit) { \
+ size = al_next_power_of_two(size + 1); \
+ } else { \
+ size = (size + al_grow_limit) & ~al_grow_limit; \
+ } \
+ if (a->alloc >= size) return; \
+ a->data = (!a->data) ? al_malloc(sizeof(T) * size) : al_realloc(a->data, sizeof(T) * size); \
+ a->alloc = size; \
+ } \
+ static inline void al_array_free(N, __VA_ARGS__)(array(N, __VA_ARGS__) *a) \
+ { \
+ if (a->data != NULL) al_free(a->data); \
+ }
+
+#define AL_ARRAY_DEFINE(N, T) \
+ typedef struct array(N) array(N); \
+ struct array(N) { \
+ u32 size; \
+ u32 alloc; \
+ T *data; \
+ }; \
+ static inline void al_array_init(N)(array(N) *a) \
+ { \
+ a->size = 0; \
+ a->alloc = 0; \
+ a->data = NULL; \
+ } \
+ static inline T *al_array_data(N)(array(N) *a) \
+ { \
+ return a->data; \
+ } \
+ AL_ARRAY_DEFINE_FUNCTIONS(N, T,) \
+ static inline void al_array_reserve(N)(array(N) *a, u32 size) \
+ { \
+ _al_array_reserve_internal(N)(a, size); \
+ }
+
+#define AL_ARRAY_INLINE_STORAGE_DEFINE(N, T, S) \
+ typedef struct array(N, S) array(N, S); \
+ struct array(N, S) { \
+ u32 size; \
+ u32 alloc; \
+ T *data; \
+ T inline_data[S]; \
+ }; \
+ static inline void al_array_init(N, S)(array(N, S) *a) \
+ { \
+ a->size = 0; \
+ a->alloc = S; \
+ a->data = NULL; \
+ } \
+ static inline T *al_array_data(N, S)(array(N, S) *a) \
+ { \
+ if (a->data) return a->data; \
+ else return a->inline_data; \
+ } \
+ AL_ARRAY_DEFINE_FUNCTIONS(N, T, S) \
+ static inline void al_array_reserve(N, S)(array(N, S) *a, u32 size) \
+ { \
+ if (S >= size) return; \
+ bool copy_inline = a->data == NULL; \
+ _al_array_reserve_internal(N, S)(a, size); \
+ if (UNLIKELY(copy_inline)) { \
+ al_memcpy(a->data, a->inline_data, sizeof(T) * a->size); \
+ } \
+ }
+
+#endif // _AL_ARRAY_TYPED_H
diff --git a/include/al/atomic.h b/include/al/atomic.h
new file mode 100644
index 0000000..5a1b495
--- /dev/null
+++ b/include/al/atomic.h
@@ -0,0 +1,42 @@
+#ifndef _AL_ATOMIC_H
+#define _AL_ATOMIC_H
+
+#include <c89atomic.h>
+
+#include "types.h"
+
+#define AL_ATOMIC_RELAXED c89atomic_memory_order_relaxed
+#define AL_ATOMIC_CONSUME c89atomic_memory_order_consume
+#define AL_ATOMIC_ACQUIRE c89atomic_memory_order_acquire
+#define AL_ATOMIC_RELEASE c89atomic_memory_order_release
+#define AL_ATOMIC_ACQ_REL c89atomic_memory_order_acq_rel
+#define AL_ATOMIC_SEQ_CST c89atomic_memory_order_seq_cst
+
+#define al_atomic_ptr_load(p, order) c89atomic_load_explicit_ptr(((volatile void **)p), order)
+#define al_atomic_ptr_store(p, v, order) c89atomic_store_explicit_ptr(((volatile void **)p), (void *)v, order)
+
+#define atomic_size_t volatile c89atomic_uint64
+#define al_atomic_size_t_load(p, order) ((size_t)c89atomic_load_explicit_64(p, order))
+#define al_atomic_size_t_store(p, v, order) c89atomic_store_explicit_64(p, ((size_t)v), order)
+
+#define atomic_s64 volatile c89atomic_int64
+#define al_atomic_s64_load(p, order) ((s64)c89atomic_load_explicit_i64(p, order))
+#define al_atomic_s64_store(p, v, order) c89atomic_store_explicit_i64(p, ((s64)v), order)
+
+#define atomic_f64 volatile f64
+#define al_atomic_f64_load(p, order) ((f64)c89atomic_load_explicit_f64(p, order))
+#define al_atomic_f64_store(p, v, order) c89atomic_store_explicit_f64(p, ((f64)v), order)
+
+#define atomic_s32 volatile c89atomic_int32
+#define al_atomic_s32_load(p, order) ((s32)c89atomic_load_explicit_i32(p, order))
+#define al_atomic_s32_store(p, v, order) c89atomic_store_explicit_i32(p, ((s32)v), order)
+
+#define atomic_u8 volatile c89atomic_uint8
+#define al_atomic_u8_load(p, order) c89atomic_load_explicit_8(p, order)
+#define al_atomic_u8_store(p, v, order) c89atomic_store_explicit_8(p, ((u8)v), order)
+
+#define atomic_bool volatile c89atomic_uint8
+#define al_atomic_bool_load(p, order) c89atomic_load_explicit_8(p, order)
+#define al_atomic_bool_store(p, v, order) c89atomic_store_explicit_8(p, ((bool)v), order)
+
+#endif // _AL_ATOMIC_H
diff --git a/include/al/lib.h b/include/al/lib.h
new file mode 100644
index 0000000..360ba38
--- /dev/null
+++ b/include/al/lib.h
@@ -0,0 +1,157 @@
+#ifndef _AL_LIB_H
+#define _AL_LIB_H
+
+#include "types.h"
+
+#if defined(__clang__) || defined(__GNUC__)
+#define AL_UNUSED_FUNCTION_PUSH \
+ _Pragma("GCC diagnostic push") \
+ _Pragma("GCC diagnostic ignored \"-Wunused-function\"")
+#define AL_UNUSED_FUNCTION_POP \
+ _Pragma("GCC diagnostic pop")
+#else
+#define AL_UNUSED_FUNCTION_PUSH
+#define AL_UNUSED_FUNCTION_POP
+#endif
+
+#define AL_USE_STDLIB 1
+
+#if AL_USE_STDLIB
+#include <stdlib.h>
+#include <string.h>
+#include <stdio.h>
+#include <stdarg.h>
+#include <assert.h>
+
+void al_malloc_init(void);
+void *al_malloc(size_t n);
+void *al_calloc(size_t n, size_t size);
+void *al_realloc(void *ptr, size_t n);
+void al_free(void *ptr);
+#ifdef HAVE_POSIX_MEMALIGN
+int al_posix_memalign(void **ptr, size_t alignment, size_t n);
+#endif
+void al_malloc_stats(u32 *current, u32 *max, u32 *total);
+void al_set_alloc(void *(*malloc_func)(size_t), void *(*calloc_func)(size_t, size_t),
+ void *(*realloc_func)(void *, size_t), void (*free_func)(void *));
+
+#define al_memcpy memcpy
+#define al_memmove memmove
+#define al_memchr memchr
+#define al_memcmp memcmp
+#define al_memset memset
+#define al_bzero bzero
+#define al_strlen strlen
+#ifdef _DEBUG_
+#define al_assert assert
+#else
+#define al_assert(expr) do { (void)sizeof(expr); } while (0)
+#endif
+
+#define al_alloc_object(type) (type *)al_calloc(1, sizeof(type))
+
+AL_UNUSED_FUNCTION_PUSH
+
+#define AL_FORCE_DISABLE_OUTPUT 0
+
+static inline s32 al_printf(const char *fmt, ...)
+{
+#if AL_FORCE_DISABLE_OUTPUT
+ return 0;
+#else
+ va_list args;
+ va_start(args, fmt);
+ s32 ret = vprintf(fmt, args);
+ va_end(args);
+ return ret;
+#endif
+}
+
+static inline void *al_memrchr(const void *s, s32 c, size_t n)
+{
+ const u8 *src = (const u8 *)s + n - 1;
+ u8 d = (u8)c;
+ while (n--) {
+ if (*src == d) return (void *)src;
+ src--;
+ }
+ return NULL;
+}
+
+AL_UNUSED_FUNCTION_POP
+#endif
+
+#ifndef LIKELY
+#ifdef HAVE_BUILTIN_EXPECT
+#define LIKELY(x) __builtin_expect((x), 1)
+#else
+#define LIKELY(x) x
+#endif
+#endif
+
+#ifndef UNLIKELY
+#ifdef HAVE_BUILTIN_EXPECT
+#define UNLIKELY(x) __builtin_expect((x), 0)
+#else
+#define UNLIKELY(x) x
+#endif
+#endif
+
+#if __GNUC__ >= 3
+#define AL_MAX(a, b) \
+({ \
+ __typeof__(a) _a = a; \
+ __typeof__(b) _b = b; \
+ _a > _b ? _a : _b; \
+})
+#define AL_MIN(a, b) \
+({ \
+ __typeof__(a) _a = a; \
+ __typeof__(b) _b = b; \
+ _a < _b ? _a : _b; \
+})
+#else
+#define AL_MAX(x, y) (((x) > (y)) ? (x) : (y))
+#define AL_MIN(x, y) (((x) < (y)) ? (x) : (y))
+#endif
+
+#define AL_CLAMP(n, lo, hi) AL_MIN(hi, AL_MAX(lo, n))
+
+#define AL_ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
+
+#ifndef _MSVC_
+#define AL_EMPTY_DEFAULT(...) AL_EMPTY_DEFAULT_0(__VA_ARGS__, , )
+#define AL_EMPTY_DEFAULT_0(FUNC, T, S, ...) FUNC(T, S)
+#define AL_PASTER_EVALUATOR(X, Y) X##_##Y
+#define AL_PASTER(X, Y) AL_PASTER_EVALUATOR(X, Y)
+#endif
+
+AL_UNUSED_FUNCTION_PUSH
+
+static inline size_t al_next_power_of_two(size_t n)
+{
+ n--;
+ n |= n >> 1;
+ n |= n >> 2;
+ n |= n >> 4;
+ n |= n >> 8;
+ return ++n;
+}
+
+static inline bool al_isspace(char c)
+{
+ return (c == ' ' || c == '\f' || c == '\n' || c == '\r' || c == '\t' || c == '\v');
+}
+
+static inline u32 al_u32_inc_wrap(u32 v)
+{
+ return (v == UINT32_MAX) ? 0 : v + 1;
+}
+
+AL_UNUSED_FUNCTION_PUSH
+
+extern size_t al_page_size;
+extern size_t al_grow_limit;
+void al_set_page_size(size_t size);
+
+#endif // _AL_LIB_H
diff --git a/include/al/list.h b/include/al/list.h
new file mode 100644
index 0000000..6e1ba29
--- /dev/null
+++ b/include/al/list.h
@@ -0,0 +1,132 @@
+#ifndef _AL_LIST_H
+#define _AL_LIST_H
+
+#include "lib.h"
+
+#define AL_LIST_DEFINE(type, name) \
+ AL_UNUSED_FUNCTION_PUSH \
+ typedef struct list_##name list_##name; \
+ struct list_##name { \
+ type value; \
+ list_##name *next; \
+ list_##name *prev; \
+ }; \
+ static inline list_##name *list_new_node_##name(type value) { \
+ list_##name *node = (list_##name *)al_malloc(sizeof(list_##name)); \
+ node->value = value; \
+ return node; \
+ } \
+ static inline void list_free_node_##name(list_##name *l) { \
+ free(l); \
+ } \
+ static inline list_##name *list_last_##name(list_##name *l) { \
+ if (l) { \
+ while (l->next) l = l->next; \
+ } \
+ return l; \
+ } \
+ static inline list_##name *list_nth_##name(list_##name *l, u32 n) { \
+ while (n-- > 0 && l) l = l->next; \
+ return l; \
+ } \
+ static inline list_##name *list_append_##name(list_##name *l, type value) { \
+ list_##name *node = list_new_node_##name(value); \
+ node->next = NULL; \
+ if (l) { \
+ list_##name *last = list_last_##name(l); \
+ node->prev = last; \
+ last->next = node; \
+ return l; \
+ } else { \
+ node->prev = NULL; \
+ return node; \
+ } \
+ } \
+ static inline list_##name *list_prepend_##name(list_##name *l, type value) { \
+ list_##name *node = list_new_node_##name(value); \
+ node->next = l; \
+ node->prev = NULL; \
+ if (l) l->prev = node; \
+ return node; \
+ } \
+ static inline list_##name *list_insert_before_##name(list_##name *l, list_##name *s, type value) { \
+ if (!l) { \
+ list_##name *node = list_new_node_##name(value); \
+ node->next = NULL; \
+ node->prev = NULL; \
+ return node; \
+ } else if (!s) { \
+ return list_append_##name(l, value); \
+ } else if (s == l) { \
+ return list_prepend_##name(l, value); \
+ } else { \
+ list_##name *node = list_new_node_##name(value); \
+ s->prev->next = node; \
+ node->prev = s->prev; \
+ node->next = s; \
+ s->prev = node; \
+ return l; \
+ } \
+ } \
+ static inline list_##name *list_insert_after_##name(list_##name *l, list_##name *s, type value) { \
+ if (!l) { \
+ list_##name *node = list_new_node_##name(value); \
+ node->next = NULL; \
+ node->prev = NULL; \
+ return node; \
+ } else if (!s || !s->next) { \
+ return list_append_##name(l, value); \
+ } else { \
+ list_##name *node = list_new_node_##name(value); \
+ s->next->prev = node; \
+ node->next = s->next; \
+ node->prev = s; \
+ s->next = node; \
+ return l; \
+ } \
+ } \
+ static inline list_##name *list_remove_##name(list_##name *l, list_##name *n) { \
+ if (!l) return l; \
+ else if (l == n) { \
+ if (n->next) { \
+ n->next->prev = NULL; \
+ } \
+ l = n->next; \
+ list_free_node_##name(n); \
+ } else { \
+ n->prev->next = n->next; \
+ if (n->next) { \
+ n->next->prev = n->prev; \
+ } \
+ list_free_node_##name(n); \
+ } \
+ return l; \
+ } \
+ AL_UNUSED_FUNCTION_POP \
+
+#define list(name) list_##name
+
+// Get element at nth position.
+#define al_list_nth(name, list, n) list_nth_##name(list, n)
+// Get the last element in the list.
+#define al_list_last(name, list) list_last_##name(list)
+
+// Append value to the end of the list.
+// If list is NULL, create new list.
+#define al_list_append(name, list, value) list_append_##name(list, value)
+// Prepend value to the front of the list.
+// If list is NULL, create new list.
+#define al_list_prepend(name, list, value) list_prepend_##name(list, value)
+
+// Insert value before/after sibling.
+// If list is NULL, create and return new list.
+// If sibiling is NULL (or sibiling->next in after case) append the value to the end of the list.
+#define al_list_insert_before(name, list, sibling, value) list_insert_before_##name(list, sibling, value)
+#define al_list_insert_after(name, list, sibling, value) list_insert_after_##name(list, sibling, value)
+
+#define al_list_remove(name, list, node) list_remove_##name(list, node)
+
+#define al_list_foreach(name, list, node) \
+ for (list_##name *node = list; node; node = node->next)
+
+#endif // _AL_LIST_H
diff --git a/include/al/log.h b/include/al/log.h
new file mode 100644
index 0000000..8723635
--- /dev/null
+++ b/include/al/log.h
@@ -0,0 +1,31 @@
+#ifndef _AL_LOG_H
+#define _AL_LOG_H
+
+#include <al/types.h>
+#include <al/lib.h>
+
+#define __FILENAME__ (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__)
+
+void al_set_print(void *userdata, s32 (*print_func)(void *, char *));
+
+s32 _al_log(const char *level, const char *section, const char *name, const s32 line,
+ const char *func, const char *fmt, ...);
+s32 _al_logv(const char *level, const char *section, const char *name, const s32 line,
+ const char *func, const char *fmt, va_list args);
+s32 _al_log_nop(const char *section, const char *fmt, ...);
+
+#define al_log(level, section, fmt, ...) \
+ _al_log(#level, section, __FILENAME__, __LINE__, __FUNCTION__, fmt, ##__VA_ARGS__)
+#define al_logv(level, section, fmt, args) \
+ _al_logv(#level, section, __FILENAME__, __LINE__, __FUNCTION__, fmt, args)
+
+#define al_log_info(section, fmt, ...) al_log(info, section, fmt, ##__VA_ARGS__)
+#define al_log_warn(section, fmt, ...) al_log(warn, section, fmt, ##__VA_ARGS__)
+#define al_log_error(section, fmt, ...) al_log(error, section, fmt, ##__VA_ARGS__)
+#ifdef _DEBUG_
+#define al_log_debug(section, fmt, ...) al_log(debug, section, fmt, ##__VA_ARGS__)
+#else
+#define al_log_debug(section, fmt, ...) _al_log_nop(section, fmt, ##__VA_ARGS__)
+#endif
+
+#endif //_AL_LOG_H
diff --git a/include/al/random.h b/include/al/random.h
new file mode 100644
index 0000000..f5210b6
--- /dev/null
+++ b/include/al/random.h
@@ -0,0 +1,28 @@
+#ifndef _AL_RANDOM_H
+#define _AL_RANDOM_H
+
+#include "lib.h"
+
+#if AL_USE_STDLIB
+#include <time.h>
+#endif
+
+static u16 _al_rand_value = 1;
+
+AL_UNUSED_FUNCTION_PUSH
+
+static void al_rand_init(void)
+{
+ srand((u32)time(NULL));
+}
+
+// TEMP: "Random" that guarantees we wont get collisions for the current use-case.
+static u16 al_rand_u16(void)
+{
+ if (_al_rand_value == UINT16_MAX) _al_rand_value = 1;
+ return _al_rand_value++;
+}
+
+AL_UNUSED_FUNCTION_POP
+
+#endif // _AL_RANDOM_H
diff --git a/include/al/ring_buffer.h b/include/al/ring_buffer.h
new file mode 100644
index 0000000..359d087
--- /dev/null
+++ b/include/al/ring_buffer.h
@@ -0,0 +1,31 @@
+#ifndef _AL_RING_BUFFER_H
+#define _AL_RING_BUFFER_H
+
+#include "types.h"
+#include "atomic.h"
+
+struct al_ring_buffer {
+ u8 *start;
+ u8 *end;
+ volatile u8 *read;
+ volatile u8 *write;
+};
+
+void al_ring_buffer_init(struct al_ring_buffer *buf, u8 *data, size_t length);
+
+size_t al_ring_buffer_space(struct al_ring_buffer *buf);
+u8 *al_ring_buffer_write_chunk(struct al_ring_buffer *buf, size_t *size);
+void al_ring_buffer_append(struct al_ring_buffer *buf, u8 *ptr, size_t n);
+
+size_t al_ring_buffer_occupied(struct al_ring_buffer *buf);
+u8 *al_ring_buffer_read_chunk(struct al_ring_buffer *buf, size_t *size);
+void al_ring_buffer_consume(struct al_ring_buffer *buf, u8 *ptr, size_t n);
+
+size_t al_ring_buffer_write(struct al_ring_buffer *buf, u8 *data, size_t n);
+size_t al_ring_buffer_read(struct al_ring_buffer *buf, u8 *ptr, size_t n);
+size_t al_ring_buffer_peek(struct al_ring_buffer *buf, u8 *ptr, size_t offset, size_t n);
+size_t al_ring_buffer_discard(struct al_ring_buffer *buf, size_t n);
+
+void al_ring_buffer_reset(struct al_ring_buffer *buf);
+
+#endif // _AL_RING_BUFFER_H
diff --git a/include/al/str.h b/include/al/str.h
new file mode 100644
index 0000000..a43d4ba
--- /dev/null
+++ b/include/al/str.h
@@ -0,0 +1,161 @@
+#ifndef _AL_STR_H
+#define _AL_STR_H
+
+#include "lib.h"
+
+typedef struct {
+ char *data;
+ u32 len;
+ u32 alloc;
+} str;
+
+#define AL_STR_EMPTY (str){ NULL, 0, 0 }
+
+#define al_str_at(s, i) (s)->data[i]
+#define al_str_atr(s, i) (s)->data[(s)->len + i]
+
+#ifndef __cplusplus
+#define al_str_c(s) (&((str){ ((char *)s), (u32)al_strlen(s), 0 }))
+#define al_str_w(s, i, n) (&((str){ &(s)[i], ((u32)n), 0 }))
+#define al_str_substr(s, start, end) al_str_w((s)->data, start, ((end) - (start)))
+#else
+#define al_str_cc(s) (((str){ ((char *)s), (u32)al_strlen(s), 0 }))
+#define al_str_ww(s, i, n) (((str){ &(s)[i], ((u32)n), 0 }))
+#define al_str_substr(s, start, end) al_str_ww((s)->data, start, ((end) - (start)))
+#endif
+
+#define AL_STR_FMT "%.*s"
+#define AL_STR_PRINTF(s) (s)->len, (s)->data
+#define AL_STR_PRINTF_MAXLEN(s, l) AL_MIN((s)->len, (l)), (s)->data
+
+AL_UNUSED_FUNCTION_PUSH
+
+static inline void al_str_free(str *s)
+{
+ if (s->alloc > 0) al_free(s->data);
+}
+
+static inline u32 al_str_reserve(str *s, u32 size)
+{
+ if (size < al_grow_limit) {
+ size = (u32)al_next_power_of_two(size + 1);
+ } else {
+ size = (u32)((size + al_grow_limit) & ~al_grow_limit);
+ }
+
+ if (size <= s->alloc) return s->alloc;
+
+ s->data = (char *)((!s->data) ? al_malloc(size) : al_realloc(s->data, size));
+
+ return size;
+}
+
+static inline void al_str_sized(str *s, u32 size)
+{
+ *s = AL_STR_EMPTY;
+ s->alloc = al_str_reserve(s, size);
+}
+
+static inline void al_str_clone(str *dest, str *src)
+{
+ al_str_sized(dest, src->len);
+ al_memcpy(dest->data, src->data, src->len);
+ dest->len = src->len;
+}
+
+static inline void al_str_from(str *s, const char *c_str)
+{
+ if (!c_str) {
+ *s = AL_STR_EMPTY;
+ return;
+ }
+ size_t len = al_strlen(c_str);
+ al_str_sized(s, (u32)len);
+ al_memcpy(s->data, c_str, len);
+ s->len = (u32)len;
+}
+
+// Returned c_str must be free'd by the user.
+static inline char *al_str_to_c_str(str *s)
+{
+ char *c = (char *)al_malloc(s->len + 1);
+ al_memcpy(c, s->data, s->len);
+ c[s->len] = '\0';
+ return c;
+}
+
+static inline void al_str_cat(str *s, str *c)
+{
+ s->alloc = al_str_reserve(s, s->len + c->len);
+ al_memcpy(s->data + s->len, c->data, c->len);
+ s->len += c->len;
+}
+
+static inline void al_str_insert(str *s, str *c, u32 i)
+{
+ s->alloc = al_str_reserve(s, s->len + c->len);
+ char *_i = s->data + i;
+ al_memmove(_i + c->len, _i, s->len - i);
+ al_memcpy(_i, c->data, c->len);
+ s->len += c->len;
+}
+
+static inline void al_str_prepend(str *s, str *c)
+{
+ al_str_insert(s, c, 0);
+}
+
+static inline s32 al_str_cmp(str *s, str *c, u32 start, u32 len)
+{
+ if (start + len > s->len || len > c->len) {
+ return -1;
+ }
+ return al_memcmp(s->data + start, c->data, len);
+}
+
+static inline bool al_str_eq(str *s, str *c)
+{
+ return al_str_cmp(s, c, 0, s->len > c->len ? s->len : c->len) == 0;
+}
+
+static inline s32 al_str_find(str *s, char c)
+{
+ char *loc = (char *)al_memchr(s->data, c, s->len);
+ if (!loc) return -1;
+ return (s32)(loc - s->data);
+}
+
+static inline s32 al_str_rfind(str *s, char c)
+{
+ char *loc = (char *)al_memrchr(s->data, c, s->len);
+ if (!loc) return -1;
+ return (s32)(loc - s->data);
+}
+
+// extremely slow.
+static inline s32 al_str_find_str(str *s, str *c)
+{
+ if (c->len > s->len) return -1;
+ for (u32 i = 0; i < s->len - c->len; i++) {
+ if (s->data[i] != c->data[0]) continue;
+ if (al_str_cmp(s, c, i, c->len) == 0) {
+ return i;
+ }
+ }
+ return -1;
+}
+
+static inline u32 al_str_hash(str *s)
+{
+ u32 hash = 5381;
+ for (u32 i = 0; i < s->len; i++) {
+ hash = ((hash << 5) + hash) + s->data[i]; /* hash * 33 + c */
+ }
+ return hash;
+}
+
+s64 al_str_to_long(str *s, s32 base);
+bool al_str_tok(str *s, char sep, str *tok);
+bool al_str_get_line(str *buffer, str *line);
+
+#endif // _AL_STR_H
diff --git a/include/al/test.h b/include/al/test.h
new file mode 100644
index 0000000..41395ce
--- /dev/null
+++ b/include/al/test.h
@@ -0,0 +1,142 @@
+#ifndef _AL_TEST_H
+#define _AL_TEST_H
+
+#include "types.h"
+#include "lib.h"
+
+#define AL_TEST_START_GROUP(name) \
+ al_printf("Group \""name"\":\n")
+
+#define AL_TEST_END_GROUP() \
+ return true
+
+#define AL_TEST_RUN(test) \
+ if (!test()) return false;
+
+#define AL_TEST_START(name) \
+ al_printf(" "name"...")
+
+#define AL_TEST_END() \
+ al_printf("OK\n"); \
+ return true
+
+#define AL_TEST_DEF(a, b, T, cmp, explain) \
+ do { \
+ AL_TEST_TYPE_##T _a = a; \
+ AL_TEST_TYPE_##T _b = b; \
+ if (cmp) { \
+ al_printf("FAIL "#a"("AL_TEST_FMT_##T") "explain" "#b"("AL_TEST_FMT_##T")\n", \
+ AL_TEST_PRINTF_##T(_a), AL_TEST_PRINTF_##T(_b)); \
+ return false; \
+ } \
+ } while (0)
+
+#define AL_TEST_EQ(a, b, T) \
+ AL_TEST_DEF(a, b, T, (AL_TEST_CMP_##T(_a, _b) != 0), \
+ "is not equal to")
+
+#define AL_TEST_NEQ(a, b, T) \
+ AL_TEST_DEF(a, b, T, (AL_TEST_CMP_##T(_a, _b) == 0), \
+ "is equal to")
+
+#define AL_TEST_GT(a, b, T) \
+ AL_TEST_DEF(a, b, T, (AL_TEST_CMP_##T(_a, _b) != 1), \
+ "is less than or equal to")
+
+#define AL_TEST_GTE(a, b, T) \
+ AL_TEST_DEF(a, b, T, (AL_TEST_CMP_##T(_a, _b) != 0 && AL_TEST_CMP_##T(_a, _b) != 1), \
+ "is less than")
+
+#define AL_TEST_LT(a, b, T) \
+ AL_TEST_DEF(a, b, T, (AL_TEST_CMP_##T(_a, _b) != -1), \
+ "is greater than or equal to")
+
+#define AL_TEST_LTE(a, b, T) \
+ AL_TEST_DEF(a, b, T, (AL_TEST_CMP_##T(_a, _b) != 0 && AL_TEST_CMP_##T(_a, _b) != -1), \
+ "is greater than")
+
+#define AL_TEST_TRUE(a) \
+ AL_TEST_DEF(a, true, bool, (AL_TEST_CMP_bool(_a, _b) != 0), \
+ "is not")
+
+#define AL_TEST_FALSE(a) \
+ AL_TEST_DEF(a, false, bool, (AL_TEST_CMP_bool(_a, _b) != 0), \
+ "is not")
+
+static inline s32 al_test_bool_cmp(bool a, bool b)
+{
+ return (a == b) ? 0 : 1;
+}
+
+#define AL_TEST_CMP_bool al_test_bool_cmp
+#define AL_TEST_TYPE_bool bool
+
+#define AL_TEST_FMT_bool "%s"
+#define AL_TEST_PRINTF_bool(b) (b ? "true" : "false")
+
+static inline s32 al_test_u32_cmp(u32 a, u32 b)
+{
+ if (a == b) return 0;
+ else if (a < b) return -1;
+ else return 1;
+}
+
+#define AL_TEST_CMP_u32 al_test_u32_cmp
+#define AL_TEST_TYPE_u32 u32
+
+#define AL_TEST_FMT_u32 "%d"
+#define AL_TEST_PRINTF_u32(i) i
+
+static inline s32 al_test_s32_cmp(s32 a, s32 b)
+{
+ if (a == b) return 0;
+ else if (a < b) return -1;
+ else return 1;
+}
+
+#define AL_TEST_CMP_s32 al_test_s32_cmp
+#define AL_TEST_TYPE_s32 s32
+
+#define AL_TEST_FMT_s32 "%i"
+#define AL_TEST_PRINTF_s32(i) i
+
+static inline s32 al_test_size_t_cmp(size_t a, size_t b)
+{
+ if (a == b) return 0;
+ else if (a < b) return -1;
+ else return 1;
+}
+
+#define AL_TEST_CMP_s64 al_test_s64_cmp
+#define AL_TEST_TYPE_s64 s64
+
+#define AL_TEST_FMT_s64 "%li"
+#define AL_TEST_PRINTF_s64(i) i
+
+static inline s32 al_test_s64_cmp(s64 a, s64 b)
+{
+ if (a == b) return 0;
+ else if (a < b) return -1;
+ else return 1;
+}
+
+#define AL_TEST_CMP_size_t al_test_size_t_cmp
+#define AL_TEST_TYPE_size_t size_t
+
+#define AL_TEST_FMT_size_t "%zu"
+#define AL_TEST_PRINTF_size_t(i) i
+
+static inline s32 al_test_ptr_cmp(void *a, void *b)
+{
+ if (a == b) return 0;
+ else if (a < b) return -1;
+ else return 1;
+}
+
+#define AL_TEST_CMP_ptr al_test_ptr_cmp
+#define AL_TEST_TYPE_ptr void *
+
+#define AL_TEST_FMT_ptr "%p"
+#define AL_TEST_PRINTF_ptr(i) i
+
+#endif // _AL_TEST_H
diff --git a/include/al/types.h b/include/al/types.h
new file mode 100644
index 0000000..c164127
--- /dev/null
+++ b/include/al/types.h
@@ -0,0 +1,61 @@
+#ifndef _AL_TYPES_H
+#define _AL_TYPES_H
+
+#ifdef _POSIX_C_SOURCE
+#if _POSIX_C_SOURCE < 200112L
+#error "libalabaster requires _POSIX_C_SOURCE >= 200112L"
+#endif
+#else
+#define _POSIX_C_SOURCE 200112L
+#endif
+
+#ifdef _XOPEN_SOURCE
+#if _XOPEN_SOURCE < 500
+#error "libalabaster requires _XOPEN_SOURCE >= 500"
+#endif
+#else
+#define _XOPEN_SOURCE 500
+#endif
+
+#if _WIN32 || _WIN64
+#if _WIN64
+#define PTR_IS_64
+#else
+#define PTR_IS_32
+#endif
+#elif __GNUC__
+#if __x86_64__ || __ppc64__
+#define PTR_IS_64
+#else
+#define PTR_IS_32
+#endif
+#elif UINTPTR_MAX > UINT_MAX
+#define PTR_IS_64
+#else
+#define PTR_IS_32
+#endif
+
+#include <stdint.h>
+#include <stddef.h>
+#include <stdbool.h>
+#include <sys/types.h>
+#include <float.h>
+
+typedef uint8_t u8;
+typedef int8_t s8;
+typedef uint16_t u16;
+typedef int16_t s16;
+typedef uint32_t u32;
+typedef int32_t s32;
+typedef uint64_t u64;
+typedef int64_t s64;
+
+typedef float f32;
+typedef double f64;
+
+#ifdef _MSC_VER
+#include <BaseTsd.h>
+typedef SSIZE_T ssize_t;
+#endif
+
+#endif // _AL_TYPES_H
diff --git a/include/al/wstr.h b/include/al/wstr.h
new file mode 100644
index 0000000..c2e8a07
--- /dev/null
+++ b/include/al/wstr.h
@@ -0,0 +1,81 @@
+#ifndef _AL_WSTR_H
+#define _AL_WSTR_H
+
+#include "types.h"
+#include "lib.h"
+#include "str.h"
+
+#include <wchar.h>
+
+typedef struct {
+ wchar_t *data;
+ u32 len;
+ u32 alloc;
+} wstr;
+
+#define al_wstr_at(w, i) (w)->data[i]
+#define al_wstr_atr(w, i) (w)->data[(w)->len + i]
+
+#define AL_WSTR_FMT "%.*ls"
+#define AL_WSTR_PRINTF(w) (w)->len, (w)->data
+#define AL_WSTR_PRINTF_MAXLEN(w, l) AL_MIN((w)->len, (l)), (w)->data
+
+static inline s32 al_wchar_width(wchar_t wc)
+{
+ return wcwidth(wc);
+}
+
+static inline void al_wstr_free(wstr *w)
+{
+ if (w->alloc > 0) al_free(w->data);
+}
+
+static inline u32 al_wstr_reserve(wstr *w, u32 size)
+{
+ if (size < al_grow_limit) {
+ size = al_next_power_of_two(size + 1);
+ } else {
+ size = (size + al_grow_limit) & ~al_grow_limit;
+ }
+
+ if (size <= w->alloc) return w->alloc;
+
+ w->data = (wchar_t *)((!w->data) ? al_malloc(size) : al_realloc(w->data, size));
+
+ return size;
+}
+
+static inline void al_wstr_sized(wstr *w, u32 size)
+{
+ w->len = 0;
+ w->alloc = 0;
+ w->data = NULL;
+ w->alloc = al_wstr_reserve(w, size * sizeof(wchar_t));
+ al_memset(w->data, 0, w->alloc);
+}
+
+static inline void al_wstr_from_cstr(wstr *w, char *c)
+{
+ size_t n = mbstowcs(NULL, c, 0) + 1;
+ al_wstr_sized(w, n);
+ w->len = mbstowcs(w->data, c, n);
+ w->data[w->len] = L'\0';
+}
+
+static inline void al_wstr_from_str(wstr *w, str *s)
+{
+ char *c_str = al_str_to_c_str(s);
+ al_wstr_from_cstr(w, c_str);
+ al_free(c_str);
+}
+
+static inline s32 al_wstr_width(wstr *w)
+{
+ s32 width = 0;
+ for (u32 i = 0; i < w->len; i++) {
+ width += al_wchar_width(al_wstr_at(w, i));
+ }
+ return width;
+}
+
+#endif // _AL_WSTR_H
diff --git a/meson.build b/meson.build
new file mode 100644
index 0000000..6e5ac71
--- /dev/null
+++ b/meson.build
@@ -0,0 +1,52 @@
+project('libalabaster', 'c', version: '0.1',
+ default_options: ['warning_level=2', 'c_std=c99'])
+
+compiler = meson.get_compiler('c')
+
+is_debug = get_option('buildtype').startswith('debug')
+is_msvc_specifically = compiler.cmd_array()[0] == 'cl'
+
+supports_needed_gnu_extensions = not is_msvc_specifically
+
+have_posix_memalign = compiler.has_function(
+ 'posix_memalign', prefix: '#define _POSIX_C_SOURCE 200112L\n#include <stdlib.h>')
+have_builtin_expect = compiler.has_function('__builtin_expect')
+
+alabaster_args = ['-D_POSIX_C_SOURCE=200112L', '-D_XOPEN_SOURCE=500']
+if is_debug
+ alabaster_args += ['-D_DEBUG_']
+endif
+if supports_needed_gnu_extensions
+ alabaster_args += ['-DHAVE_GNU_EXTENSIONS']
+endif
+if have_posix_memalign
+ alabaster_args += ['-DHAVE_POSIX_MEMALIGN']
+endif
+if have_builtin_expect
+ alabaster_args += ['-DHAVE_BUILTIN_EXPECT']
+endif
+if is_msvc_specifically
+ alabaster_args += ['/arch:AVX']
+endif
+
+c89atomic = subproject('c89atomic').get_variable('c89atomic')
+
+alabaster_src = ['src/lib.c', 'src/str.c', 'src/log.c', 'src/ring_buffer.c']
+alabaster_inc = include_directories('include')
+
+alabaster = declare_dependency(include_directories: alabaster_inc,
+ dependencies: [c89atomic], sources: alabaster_src,
+ compile_args: alabaster_args)
+
+if get_option('build-tests') and not meson.is_subproject()
+ tests_src = [
+ 'tests/main.c',
+ 'tests/array.c',
+ 'tests/str.c',
+ 'tests/ring_buffer.c'
+ ]
+ if supports_needed_gnu_extensions
+ tests_src += ['tests/array_typed.c']
+ endif
+ executable('tests', tests_src, dependencies: [alabaster], install: false)
+endif
diff --git a/meson_options.txt b/meson_options.txt
new file mode 100644
index 0000000..891bc02
--- /dev/null
+++ b/meson_options.txt
@@ -0,0 +1 @@
+option('build-tests', type: 'boolean', value: true)
diff --git a/src/array.c b/src/array.c
new file mode 100644
index 0000000..ffdc36b
--- /dev/null
+++ b/src/array.c
@@ -0,0 +1,105 @@
+#include "../include/al/lib.h"
+#include "../include/al/array_sort.h"
+
+// based on: https://github.com/lifthrasiir/angolmois/blob/master/angolmois.c#L79
+
+#define array(type) \
+ struct { \
+ u32 size; \
+ u32 alloc; \
+ type *data; \
+ }
+
+#define al_array_init(arr) ((arr).size = 0, (arr).alloc = 0, (arr).data = NULL)
+#define al_array_item_size(arr) (sizeof(*(arr).data)) // NOLINT(bugprone-sizeof-expression)
+
+#define al_array_at(arr, i) ((arr).data[i])
+#define al_array_last(arr) al_array_at(arr, (arr).size - 1)
+
+#define al_array_reserve(arr, size) \
+ ((arr).alloc = _al_array_reserve((void *)&(arr).data, al_array_item_size(arr), (arr).alloc, size))
+
+#define al_array_clone(dest, src) \
+ do { \
+ al_array_init(dest); \
+ al_array_reserve(dest, (src).size); \
+ al_memcpy(&al_array_at(dest, 0), &al_array_at(src, 0), \
+ al_array_item_size(dest) * (dest).size); \
+ (dest).size = (src).size; \
+ } while (0)
+
+#define al_array_push(arr, item) \
+ do { \
+ al_array_reserve(arr, (arr).size + 1); \
+ al_array_at(arr, (arr).size++) = item; \
+ } while (0)
+
+#define al_array_insert(arr, item, i) \
+ do { \
+ if ((arr).size > i) { \
+ al_array_reserve(arr, (arr).size + 1); \
+ al_memmove(&al_array_at(arr, i + 1), &al_array_at(arr, i), \
+ al_array_item_size(arr) * ((arr).size - i)); \
+ (arr).size++; \
+ } else { \
+ al_array_reserve(arr, i + 1); \
+ (arr).size = i + 1; \
+ } \
+ al_array_at(arr, i) = item; \
+ } while (0)
+
+#define al_array_pop_at(arr, i, r) \
+ do { \
+ r = al_array_at((arr), i); \
+ al_array_remove_at((arr), i); \
+ } while (0)
+
+#define al_array_pop(arr) (al_array_at(arr, --(arr).size))
+#define al_array_pop_front(arr) al_array_pop_at(arr, 0)
+
+#define al_array_remove_at(arr, i) \
+ do { \
+ if (i == (arr).size - 1) { \
+ (arr).size--; \
+ } else { \
+ al_memmove(&al_array_at(arr, i), &al_array_at(arr, i + 1), \
+ al_array_item_size(arr) * (--(arr).size - i)); \
+ } \
+ } while (0)
+
+#define al_array_sort(arr, type, cmp) \
+ AL_INSERSION_SORT((arr).data, type, (arr).size, cmp)
+
+#define al_array_free(arr) \
+ if ((arr).alloc) AL_ARRAY_FREE((arr).data)
+
+#define al_array_foreach(arr, i, item) \
+ for (u32 i = 0; (i < (arr).size && (item = al_array_at(arr, i), 1)); i++)
+
+#define al_array_foreach_ptr(arr, i, item) \
+ for (u32 i = 0; (i < (arr).size && (item = &al_array_at(arr, i), 1)); i++)
+
+#define al_array_remove_at_iter(arr, i) \
+ do { \
+ al_array_remove_at(arr, i); \
+ i--; \
+ } while (0)
+
+AL_UNUSED_FUNCTION_PUSH
+
+static inline u32 _al_array_reserve(void **ptr, u32 item_size, u32 prev_size, u32 size)
+{
+ if (size < al_grow_limit) {
+ size = (u32)al_next_power_of_two(size + 1);
+ } else {
+ size = (u32)((size + al_grow_limit) & ~al_grow_limit);
+ }
+
+ if (size <= prev_size) return prev_size;
+
+ *ptr = (!*ptr) ? AL_ARRAY_MALLOC(item_size * size) : AL_ARRAY_REALLOC(*ptr, item_size * size);
+
+ return size;
+}
+
+AL_UNUSED_FUNCTION_POP
diff --git a/src/lib.c b/src/lib.c
new file mode 100644
index 0000000..2df40d3
--- /dev/null
+++ b/src/lib.c
@@ -0,0 +1,184 @@
+// Testing allocation tracking stuff. (Very scuffed)
+
+#include "../include/al/lib.h"
+
+#if AL_USE_STDLIB
+static void *(*_al_malloc)(size_t) = malloc;
+static void *(*_al_calloc)(size_t, size_t) = calloc;
+static void *(*_al_realloc)(void *, size_t) = realloc;
+static void (*_al_free)(void *) = free;
+#ifdef HAVE_POSIX_MEMALIGN
+s32 (*_al_posix_memalign)(void **, size_t, size_t) = posix_memalign;
+#endif
+
+#define MEMORY_TRACKING 0
+
+#if MEMORY_TRACKING
+struct alloc_t {
+ void *ptr;
+ size_t size;
+};
+
+// Use stdlib allocation for the allocation tracking array.
+// This avoids a circular loop and also keeps allocations
+// from the memory tracking code out of the stats.
+#include "../include/al/types.h"
+#define AL_ARRAY_MALLOC malloc
+#define AL_ARRAY_REALLOC realloc
+#define AL_ARRAY_FREE free
+#include "array.c"
+
+static array(struct alloc_t) allocations;
+static u32 total_allocated = 0;
+static u32 currently_allocated = 0;
+static u32 peak_allocated = 0;
+#endif
+
+void al_malloc_init(void)
+{
+#if MEMORY_TRACKING
+ al_array_init(allocations);
+#endif
+}
+
+void *al_malloc(size_t n)
+{
+ void *ptr = _al_malloc(n);
+#if MEMORY_TRACKING
+ printf("**al_malloc(%zu)\n", n);
+ struct alloc_t a = {
+ .ptr = ptr,
+ .size = n
+ };
+ total_allocated += a.size;
+ currently_allocated += a.size;
+ if (currently_allocated > peak_allocated) {
+ peak_allocated = currently_allocated;
+ }
+ al_array_push(allocations, a);
+#endif
+ return ptr;
+}
+
+void *al_calloc(size_t n, size_t size)
+{
+ void *ptr = _al_calloc(n, size);
+#if MEMORY_TRACKING
+ printf("**al_calloc(%zu, %zu)\n", n, size);
+ struct alloc_t a = {
+ .ptr = ptr,
+ .size = n * size
+ };
+ total_allocated += a.size;
+ currently_allocated += a.size;
+ if (currently_allocated > peak_allocated) {
+ peak_allocated = currently_allocated;
+ }
+ al_array_push(allocations, a);
+#endif
+ return ptr;
+}
+
+void *al_realloc(void *ptr, size_t n)
+{
+ void *nptr = _al_realloc(ptr, n);
+#if MEMORY_TRACKING
+ printf("**al_realloc(%p, %zu)\n", ptr, n);
+ for (u32 i = 0; i < allocations.size; i++) {
+ struct alloc_t *a = &al_array_at(allocations, i);
+ if (a->ptr == ptr) {
+ currently_allocated += (ssize_t)n - (ssize_t)a->size;
+ if (currently_allocated > peak_allocated) {
+ peak_allocated = currently_allocated;
+ }
+ a->ptr = nptr;
+ a->size = n;
+ }
+ }
+ if (!ptr) {
+ struct alloc_t a = {
+ .ptr = nptr,
+ .size = n
+ };
+ currently_allocated += a.size;
+ if (currently_allocated > peak_allocated) {
+ peak_allocated = currently_allocated;
+ }
+ al_array_push(allocations, a);
+ }
+ total_allocated += n;
+#endif
+ return nptr;
+}
+
+void al_free(void *ptr)
+{
+#if MEMORY_TRACKING
+ printf("**al_free(%p)\n", ptr);
+ for (u32 i = 0; i < allocations.size; i++) {
+ struct alloc_t *a = &al_array_at(allocations, i);
+ if (a->ptr == ptr) {
+ currently_allocated -= a->size;
+ al_array_remove_at(allocations, i);
+ break;
+ }
+ }
+#endif
+ _al_free(ptr);
+}
+
+#ifdef HAVE_POSIX_MEMALIGN
+s32 al_posix_memalign(void **ptr, size_t alignment, size_t n)
+{
+ s32 res = _al_posix_memalign(ptr, alignment, n);
+#if MEMORY_TRACKING
+ size_t actual_size = (n + alignment) - ((n + alignment) % alignment);
+ printf("**al_posix_memalign(%zu, %zu(%zu))\n", alignment, n, actual_size);
+ struct alloc_t a = {
+ .ptr = *ptr,
+ .size = actual_size
+ };
+ total_allocated += actual_size;
+ currently_allocated += actual_size;
+ if (currently_allocated > peak_allocated) {
+ peak_allocated = currently_allocated;
+ }
+ al_array_push(allocations, a);
+#endif
+ return res;
+}
+#endif
+
+void al_malloc_stats(u32 *current, u32 *peak, u32 *total)
+{
+#if MEMORY_TRACKING
+ *current = currently_allocated;
+ *peak = peak_allocated;
+ *total = total_allocated;
+#else
+ *current = 0;
+ *peak = 0;
+ *total = 0;
+#endif
+}
+
+void al_set_alloc(void *(*malloc_func)(size_t), void *(*calloc_func)(size_t, size_t),
+ void *(*realloc_func)(void *, size_t), void (*free_func)(void *))
+{
+ _al_malloc = malloc_func;
+ _al_calloc = calloc_func;
+ _al_realloc = realloc_func;
+ _al_free = free_func;
+}
+#endif
+
+// Default to 4KB.
+size_t al_page_size = 0x1000;
+size_t al_grow_limit = 0xfff;
+
+void al_set_page_size(size_t size)
+{
+ al_assert(size > 0);
+ al_page_size = size;
+ al_grow_limit = al_page_size - 1;
+}
diff --git a/src/log.c b/src/log.c
new file mode 100644
index 0000000..0444ed6
--- /dev/null
+++ b/src/log.c
@@ -0,0 +1,90 @@
+#include "../include/al/log.h"
+
+#define AL_LOG_SKIP 1
+#define AL_LOG_MESSAGE_SIZE 511
+#define AL_LOG_TEMPLATE "%s:%d %s(): %s -> (%s) "
+
+static void *al_log_userdata = NULL;
+
+static s32 al_print_default(void *userdata, char *s)
+{
+ (void)userdata;
+ s32 ret = al_printf("%*.*s\n", 0, AL_LOG_MESSAGE_SIZE, s);
+ al_free(s);
+ return ret;
+}
+
+static s32 (*_al_print)(void *, char *) = al_print_default;
+
+void al_set_print(void *userdata, s32 (*print_func)(void *, char *))
+{
+ al_log_userdata = userdata;
+ _al_print = print_func;
+}
+
+#if !AL_LOG_SKIP
+static char *get_buffer(const char *level, const char *section, const char *fmt,
+ const char *name, const s32 line, const char *func, va_list args)
+{
+ char *buffer = al_calloc(1, AL_LOG_MESSAGE_SIZE + 1);
+ al_memset(buffer, 0, AL_LOG_MESSAGE_SIZE);
+ s32 prefix = snprintf(buffer, AL_LOG_MESSAGE_SIZE, AL_LOG_TEMPLATE, name, line, func, level, section);
+ prefix += vsnprintf(buffer + prefix, AL_LOG_MESSAGE_SIZE - prefix, fmt, args);
+ buffer[strcspn(buffer, "\r\n")] = '\0';
+ return buffer;
+}
+#endif
+
+s32 _al_log_nop(const char *section, const char *fmt, ...)
+{
+ (void)section;
+ (void)fmt;
+ return 0;
+}
+
+s32 _al_log(const char *level, const char *section, const char *name, const s32 line,
+ const char *func, const char *fmt, ...)
+{
+#if AL_FORCE_DISABLE_OUTPUT
+ return 0;
+#else
+ va_list args;
+ va_start(args, fmt);
+#if AL_LOG_SKIP
+ s32 ret = printf(AL_LOG_TEMPLATE, name, line, func, level, section);
+ ret += vprintf(fmt, args);
+ size_t cspn = strcspn(fmt, "\r\n");
+ if (cspn == strlen(fmt)) ret += printf("\n");
+#else
+ char *buffer = get_buffer(level, section, fmt, name, line, func, args);
+#endif
+ va_end(args);
+#if AL_LOG_SKIP
+ return ret;
+#else
+ return _al_print(al_log_userdata, buffer);
+#endif
+#endif
+}
+
+s32 _al_logv(const char *level, const char *section, const char *name, const s32 line,
+ const char *func, const char *fmt, va_list args)
+{
+#if AL_FORCE_DISABLE_OUTPUT
+ return 0;
+#else
+#if AL_LOG_SKIP
+ s32 ret = printf(AL_LOG_TEMPLATE, name, line, func, level, section);
+ ret += vprintf(fmt, args);
+ size_t cspn = strcspn(fmt, "\r\n");
+ if (cspn == strlen(fmt)) ret += printf("\n");
+#else
+ char *buffer = get_buffer(level, section, fmt, name, line, func, args);
+#endif
+#if AL_LOG_SKIP
+ return ret;
+#else
+ return _al_print(al_log_userdata, buffer);
+#endif
+#endif
+}
diff --git a/src/ring_buffer.c b/src/ring_buffer.c
new file mode 100644
index 0000000..eaff139
--- /dev/null
+++ b/src/ring_buffer.c
@@ -0,0 +1,169 @@
+#include "../include/al/lib.h"
+#include "../include/al/ring_buffer.h"
+
+// https://github.com/MusicPlayerDaemon/MPD/blob/master/src/util/RingBuffer.hxx
+// https://andrea.lattuada.me/blog/2019/the-design-and-implementation-of-a-lock-free-ring-buffer-with-contiguous-reservations.html
+
+void al_ring_buffer_init(struct al_ring_buffer *buf, u8 *data, size_t length)
+{
+ buf->start = data;
+ buf->end = data + length;
+ al_ring_buffer_reset(buf);
+}
+
+static inline u8 *previous(struct al_ring_buffer *buf, u8 *ptr)
+{
+ if (ptr == buf->start) ptr = buf->end;
+ return --ptr;
+}
+
+static inline void add(struct al_ring_buffer *buf, volatile u8 **v, u8 *ptr, size_t n)
+{
+ ptr += n;
+ al_assert(ptr <= buf->end);
+ if (ptr == buf->end) ptr = buf->start;
+ al_atomic_ptr_store(v, ptr, AL_ATOMIC_RELEASE);
+}
+
+size_t al_ring_buffer_space(struct al_ring_buffer *buf)
+{
+ u8 *wp = al_atomic_ptr_load(&buf->write, AL_ATOMIC_RELAXED);
+ u8 *rp = previous(buf, (u8 *)al_atomic_ptr_load(&buf->read, AL_ATOMIC_RELAXED));
+ return (wp <= rp) ? rp - wp : (buf->end - wp) + (rp - buf->start);
+}
+
+u8 *al_ring_buffer_write_chunk(struct al_ring_buffer *buf, size_t *size)
+{
+ u8 *wp = al_atomic_ptr_load(&buf->write, AL_ATOMIC_ACQUIRE);
+ u8 *rp = previous(buf, al_atomic_ptr_load(&buf->read, AL_ATOMIC_RELAXED));
+ *size = (wp <= rp ? rp : buf->end) - wp;
+ return wp;
+}
+
+void al_ring_buffer_append(struct al_ring_buffer *buf, u8 *ptr, size_t n)
+{
+ add(buf, &buf->write, ptr, n);
+}
+
+size_t al_ring_buffer_occupied(struct al_ring_buffer *buf)
+{
+ u8 *rp = al_atomic_ptr_load(&buf->read, AL_ATOMIC_RELAXED);
+ u8 *wp = al_atomic_ptr_load(&buf->write, AL_ATOMIC_RELAXED);
+ return (rp <= wp) ? wp - rp : (buf->end - rp) + (wp - buf->start);
+}
+
+u8 *al_ring_buffer_read_chunk(struct al_ring_buffer *buf, size_t *size)
+{
+ u8 *rp = al_atomic_ptr_load(&buf->read, AL_ATOMIC_ACQUIRE);
+ u8 *wp = al_atomic_ptr_load(&buf->write, AL_ATOMIC_RELAXED);
+ *size = (rp <= wp ? wp : buf->end) - rp;
+ return rp;
+}
+
+void al_ring_buffer_consume(struct al_ring_buffer *buf, u8 *ptr, size_t n)
+{
+ add(buf, &buf->read, ptr, n);
+}
+
+size_t al_ring_buffer_write(struct al_ring_buffer *buf, u8 *data, size_t n)
+{
+ u8 *wp = al_atomic_ptr_load(&buf->write, AL_ATOMIC_ACQUIRE);
+ u8 *rp = previous(buf, al_atomic_ptr_load(&buf->read, AL_ATOMIC_RELAXED));
+
+ size_t size = AL_MIN((wp <= rp ? rp : buf->end) - wp, (ptrdiff_t)n);
+
+ al_memcpy(wp, data, size);
+ wp += size;
+
+ if (wp >= buf->end) {
+ size_t wrap = AL_MIN(rp - buf->start, (ptrdiff_t)(n - size));
+ al_memcpy(buf->start, data + size, wrap);
+ wp = buf->start + wrap;
+ size += wrap;
+ }
+
+ al_atomic_ptr_store(&buf->write, wp, AL_ATOMIC_RELEASE);
+
+ return size;
+}
+
+size_t al_ring_buffer_read(struct al_ring_buffer *buf, u8 *ptr, size_t n)
+{
+ u8 *rp = al_atomic_ptr_load(&buf->read, AL_ATOMIC_ACQUIRE);
+ u8 *wp = al_atomic_ptr_load(&buf->write, AL_ATOMIC_RELAXED);
+
+ size_t size = AL_MIN((rp <= wp ? wp : buf->end) - rp, (ptrdiff_t)n);
+
+ al_memcpy(ptr, rp, size);
+ rp += size;
+
+ if (rp >= buf->end) {
+ size_t wrap = AL_MIN(wp - buf->start, (ptrdiff_t)(n - size));
+ al_memcpy(ptr + size, buf->start, wrap);
+ rp = buf->start + wrap;
+ size += wrap;
+ }
+
+ al_atomic_ptr_store(&buf->read, rp, AL_ATOMIC_RELEASE);
+
+ return size;
+}
+
+size_t al_ring_buffer_peek(struct al_ring_buffer *buf, u8 *ptr, size_t offset, size_t n)
+{
+ u8 *rp = al_atomic_ptr_load(&buf->read, AL_ATOMIC_RELAXED);
+ u8 *wp = al_atomic_ptr_load(&buf->write, AL_ATOMIC_RELAXED);
+
+ ptrdiff_t size;
+
+ if (rp <= wp) {
+ rp += offset;
+ size = AL_MIN(wp - rp, (ptrdiff_t)n);
+ if (size < 0) return 0;
+ al_memcpy(ptr, rp, size);
+ } else {
+ rp += offset;
+ size = AL_MIN(buf->end - rp, (ptrdiff_t)n);
+ if (size > 0) {
+ al_memcpy(ptr, rp, size);
+ n -= size;
+ rp = buf->start;
+ } else {
+ rp = buf->start - size;
+ size = 0;
+ }
+ ptrdiff_t wrap = AL_MIN(wp - rp, (ptrdiff_t)n);
+ if (wrap > 0) {
+ al_memcpy(ptr + size, rp, wrap);
+ size += wrap;
+ }
+ }
+
+ return size;
+}
+
+size_t al_ring_buffer_discard(struct al_ring_buffer *buf, size_t n)
+{
+ u8 *rp = al_atomic_ptr_load(&buf->read, AL_ATOMIC_ACQUIRE);
+ u8 *wp = al_atomic_ptr_load(&buf->write, AL_ATOMIC_RELAXED);
+
+ size_t discard = AL_MIN((rp <= wp ? wp : buf->end) - rp, (ptrdiff_t)n);
+ rp += discard;
+
+ if (rp >= buf->end) {
+ size_t wrap = AL_MIN(wp - buf->start, (ptrdiff_t)(n - discard));
+ rp = buf->start + wrap;
+ discard += wrap;
+ }
+
+ al_atomic_ptr_store(&buf->read, rp, AL_ATOMIC_RELEASE);
+
+ return discard;
+}
+
+// Not thread-safe.
+void al_ring_buffer_reset(struct al_ring_buffer *buf)
+{
+ al_atomic_ptr_store(&buf->read, buf->start, AL_ATOMIC_RELAXED);
+ al_atomic_ptr_store(&buf->write, buf->start, AL_ATOMIC_RELAXED);
+}
diff --git a/src/str.c b/src/str.c
new file mode 100644
index 0000000..a3c0946
--- /dev/null
+++ b/src/str.c
@@ -0,0 +1,113 @@
+#include "../include/al/str.h"
+
+// https://sourceware.org/git/?p=newlib-cygwin.git;a=blob;f=newlib/libc/stdlib/strtol.c;h=09d4333ed05f497ec00b7192204644349441e58d;hb=HEAD#l130
+s64 al_str_to_long(str *s, s32 base)
+{
+ u64 ret = 0;
+ u32 i = 0;
+ s32 neg = 0;
+
+ while (al_isspace(al_str_at(s, i)) && i < s->len) i++;
+
+ if (al_str_at(s, i) == '-') {
+ neg = 1;
+ i++;
+ } else if (al_str_at(s, i) == '+') {
+ i++;
+ }
+
+ if ((base == 16 && al_str_at(s, i) == '0' && // 0xbecd
+ (al_str_at(s, i + 1) == 'x' || al_str_at(s, i + 1) == 'X')) ||
+ (base == 2 && al_str_at(s, i) == '0' && // 0b01011101
+ (al_str_at(s, i + 1) == 'b' || al_str_at(s, i + 1) == 'B'))) {
+ i += 2;
+ }
+
+ u64 cutoff = neg ? -(u64)INT64_MIN : INT64_MAX;
+ s32 add_limit = cutoff % (u64)base;
+ cutoff /= (u64)base;
+
+ bool error = false;
+
+ for (; i < s->len; i++) {
+ char c = al_str_at(s, i);
+
+ if (c >= '0' && c <= '9') {
+ c -= '0';
+ } else if (c >= 'A' && c <= 'Z') {
+ c -= 'A' - 10;
+ } else if (c >= 'a' && c <= 'z') {
+ c -= 'a' - 10;
+ } else {
+ // Character not valid when expressing a number.
+ error = true;
+ break;
+ }
+
+ // Character invalid for given base or trying to add the next digit
+ // would cause an overflow/underflow.
+ if (c >= base || ret > cutoff || (ret == cutoff && c > add_limit)) {
+ error = true;
+ break;
+ }
+
+ ret = (ret * base) + c;
+ }
+
+ if (error) ret = neg ? INT64_MIN : INT64_MAX;
+ else if (neg) ret = -ret;
+
+ return ret;
+}
+
+bool al_str_tok(str *s, char sep, str *tok)
+{
+ if (tok->alloc == s->len + 1) {
+ return false;
+ }
+ u32 r = s->len - tok->alloc;
+#ifndef __cplusplus
+ s32 i = al_str_find(al_str_w(s->data, tok->alloc, r), sep);
+#else
+ str w = al_str_ww(s->data, tok->alloc, r);
+ s32 i = al_str_find(&w, sep);
+#endif
+ if (i == -1) i = r;
+ tok->data = s->data + tok->alloc;
+ tok->len = (u32)i;
+ tok->alloc += tok->len + 1;
+ return true;
+}
+
+bool al_str_get_line(str *buffer, str *line)
+{
+ s32 bytes_processed = 0;
+
+ if (line->data == NULL) {
+ *line = *buffer;
+ } else {
+ // Move to the end of the current line.
+ line->data += line->len;
+
+ bytes_processed = (s32)((line->data - buffer->data) + 1);
+
+ // If line->ptr is at the last character, we are done.
+ // Also, if bytes_processed <= 0 buffer is empty or invalid, so return.
+ if (bytes_processed >= (s32)buffer->len || bytes_processed <= 0) {
+ return false;
+ }
+
+ // Else, move line->ptr to the first character on the next line.
+ line->data++;
+ }
+
+ // Find the next newline character.
+ char *nl = (char *)memchr(line->data, '\n', buffer->len - bytes_processed);
+
+ // If there is no newline character move to the end of the buffer.
+ if (!nl) nl = buffer->data + buffer->len;
+
+ line->len = (u32)(nl - line->data);
+
+ return true;
+}
diff --git a/subprojects/c89atomic.wrap b/subprojects/c89atomic.wrap
new file mode 100644
index 0000000..2fe249e
--- /dev/null
+++ b/subprojects/c89atomic.wrap
@@ -0,0 +1,5 @@
+[wrap-git]
+url = https://github.com/mackron/c89atomic.git
+revision = cc548edae7147b31718f254e35f6052fa1559ff7
+depth = 1
+patch_directory = c89atomic
diff --git a/subprojects/packagefiles/c89atomic/meson.build b/subprojects/packagefiles/c89atomic/meson.build
new file mode 100644
index 0000000..cdf8a28
--- /dev/null
+++ b/subprojects/packagefiles/c89atomic/meson.build
@@ -0,0 +1,2 @@
+project('c89atomic', 'c', version: 'git-cc548ed')
+c89atomic = declare_dependency(include_directories: include_directories('.'))
diff --git a/tests/array.c b/tests/array.c
new file mode 100644
index 0000000..ad03c3c
--- /dev/null
+++ b/tests/array.c
@@ -0,0 +1,256 @@
+#include <al/test.h>
+#include <al/array.h>
+
+#include "array.h"
+
+struct test_t {
+ u32 i;
+};
+
+static bool array_test_init(void)
+{
+ AL_TEST_START("array_init");
+
+ array(u32) arr;
+ al_array_init(arr);
+
+ AL_TEST_EQ((void *)arr.data, NULL, ptr);
+ AL_TEST_EQ(arr.alloc, 0, u32);
+ AL_TEST_EQ(arr.size, 0, u32);
+
+ al_array_free(arr);
+
+ AL_TEST_END();
+}
+
+static bool array_test_reserve(void)
+{
+ AL_TEST_START("array_reserve");
+
+ array(u32) arr;
+ al_array_init(arr);
+
+ al_array_reserve(arr, 64);
+
+ AL_TEST_NEQ((void *)arr.data, NULL, ptr);
+ AL_TEST_EQ(arr.alloc, 128, u32);
+
+ al_array_reserve(arr, 128);
+
+ AL_TEST_NEQ((void *)arr.data, NULL, ptr);
+ AL_TEST_EQ(arr.alloc, 256, u32);
+
+ al_array_free(arr);
+
+ AL_TEST_END();
+}
+
+static bool array_test_at(void)
+{
+ AL_TEST_START("array_at");
+
+ array(u32) arr;
+ al_array_init(arr);
+
+ u32 x = 5;
+ al_array_push(arr, x);
+
+ AL_TEST_EQ(al_array_at(arr, 0), 5, u32);
+
+ al_array_free(arr);
+
+ AL_TEST_END();
+}
+
+static bool array_test_last(void)
+{
+ AL_TEST_START("array_last");
+
+ array(u32) arr;
+ al_array_init(arr);
+
+ u32 x = 1, y = 2, z = 3;
+
+ al_array_push(arr, x);
+ al_array_push(arr, y);
+ al_array_push(arr, z);
+
+ AL_TEST_EQ(al_array_last(arr), 3, u32);
+
+ al_array_free(arr);
+
+ AL_TEST_END();
+}
+
+static bool array_test_push(void)
+{
+ AL_TEST_START("array_push");
+
+ array(u32) arr;
+ al_array_init(arr);
+
+ u32 x = 4, y = 5;
+
+ al_array_push(arr, x);
+ al_array_push(arr, y);
+
+ AL_TEST_EQ(arr.size, 2, u32);
+ AL_TEST_EQ(al_array_at(arr, 0), x, u32);
+ AL_TEST_EQ(al_array_at(arr, 1), y, u32);
+
+ al_array_free(arr);
+
+ AL_TEST_END();
+}
+
+static bool array_test_pop(void)
+{
+ AL_TEST_START("array_pop");
+
+ array(u32) arr;
+ al_array_init(arr);
+
+ u32 x = 4, y = 5;
+
+ al_array_push(arr, x);
+ al_array_push(arr, y);
+
+ u32 rx, ry;
+
+ ry = al_array_pop(arr);
+
+ AL_TEST_EQ(arr.size, 1, u32);
+
+ rx = al_array_pop(arr);
+
+ AL_TEST_EQ(arr.size, 0, u32);
+ AL_TEST_EQ(rx, x, u32);
+ AL_TEST_EQ(ry, y, u32);
+
+ al_array_free(arr);
+
+ AL_TEST_END();
+}
+
+static bool array_test_remove_at(void)
+{
+ AL_TEST_START("array_remove_at");
+
+ array(u32) arr;
+ al_array_init(arr);
+ al_array_reserve(arr, 12);
+
+ u32 array_before[] = { 1, 2, 3, 4, 5, 6 };
+ u32 array_after[] = { 2, 3, 4 };
+
+ for (u32 i = 0; i < sizeof(array_before) / sizeof(u32); i++) {
+ al_array_push(arr, array_before[i]);
+ }
+
+ al_array_remove_at(arr, 0);
+ al_array_remove_at(arr, 3);
+ al_array_remove_at(arr, 3);
+
+ AL_TEST_EQ(arr.size, (u32)(sizeof(array_after) / sizeof(u32)), u32);
+
+ for (u32 i = 0; i < sizeof(array_after) / sizeof(u32); i++) {
+ AL_TEST_EQ(al_array_at(arr, i), array_after[i], u32);
+ }
+
+ al_array_free(arr);
+
+ AL_TEST_END();
+}
+
+static bool array_test_pop_at(void)
+{
+ AL_TEST_START("array_pop_at");
+
+ array(u32) arr;
+ al_array_init(arr);
+
+ u32 array_before[] = { 1, 2, 3, 4, 5, 6 };
+ u32 array_after[] = { 2, 3, 4 };
+
+ for (u32 i = 0; i < sizeof(array_before) / sizeof(u32); i++) {
+ al_array_push(arr, array_before[i]);
+ }
+
+ u32 x, y, z;
+
+ al_array_pop_at(arr, 0, x);
+ al_array_pop_at(arr, 3, y);
+ al_array_pop_at(arr, 3, z);
+
+ AL_TEST_EQ(x, array_before[0], u32);
+ AL_TEST_EQ(y, array_before[4], u32);
+ AL_TEST_EQ(z, array_before[5], u32);
+
+ AL_TEST_EQ(arr.size, (u32)(sizeof(array_after) / sizeof(u32)), u32);
+
+ for (u32 i = 0; i < sizeof(array_after) / sizeof(u32); i++) {
+ AL_TEST_EQ(al_array_at(arr, i), array_after[i], u32);
+ }
+
+ al_array_free(arr);
+
+ AL_TEST_END();
+}
+
+static bool array_test_insert(void)
+{
+ AL_TEST_START("array_insert");
+
+ array(struct test_t) arr;
+ al_array_init(arr);
+
+ struct test_t x = {
+ .i = 5
+ };
+
+ struct test_t y = {
+ .i = 5
+ };
+
+ struct test_t z = {
+ .i = 5
+ };
+
+ al_array_insert(arr, x, 1);
+ al_array_insert(arr, y, 0);
+ al_array_insert(arr, z, 1);
+
+ AL_TEST_EQ(al_array_at(arr, 3).i, x.i, u32);
+ AL_TEST_EQ(al_array_at(arr, 0).i, y.i, u32);
+ AL_TEST_EQ(al_array_at(arr, 1).i, z.i, u32);
+
+ AL_TEST_EQ(arr.size, 4, u32);
+
+ al_array_remove_at(arr, 3);
+
+ AL_TEST_EQ(arr.size, 3, u32);
+
+ AL_TEST_EQ(al_array_at(arr, 0).i, y.i, u32);
+ AL_TEST_EQ(al_array_at(arr, 1).i, z.i, u32);
+
+ al_array_free(arr);
+
+ AL_TEST_END();
+}
+
+bool array_tests_run(void)
+{
+ AL_TEST_START_GROUP("array");
+
+ AL_TEST_RUN(array_test_init);
+ AL_TEST_RUN(array_test_reserve);
+ AL_TEST_RUN(array_test_at);
+ AL_TEST_RUN(array_test_last);
+ AL_TEST_RUN(array_test_push);
+ AL_TEST_RUN(array_test_pop);
+ AL_TEST_RUN(array_test_remove_at);
+ AL_TEST_RUN(array_test_pop_at);
+ AL_TEST_RUN(array_test_insert);
+
+ AL_TEST_END_GROUP();
+}
diff --git a/tests/array.h b/tests/array.h
new file mode 100644
index 0000000..881f936
--- /dev/null
+++ b/tests/array.h
@@ -0,0 +1,8 @@
+#ifndef _AL_ARRAY_TESTS_H
+#define _AL_ARRAY_TESTS_H
+
+#include <al/types.h>
+
+bool array_tests_run(void);
+
+#endif // _AL_ARRAY_TESTS_H
diff --git a/tests/array_typed.c b/tests/array_typed.c
new file mode 100644
index 0000000..21620e5
--- /dev/null
+++ b/tests/array_typed.c
@@ -0,0 +1,261 @@
+#include <al/test.h>
+#include <al/array_typed.h>
+
+#include "array_typed.h"
+
+struct test_t {
+ u32 i;
+};
+
+AL_ARRAY_INLINE_STORAGE_DEFINE(u32, u32, 64)
+AL_ARRAY_DEFINE(u32, u32)
+AL_ARRAY_INLINE_STORAGE_DEFINE(test_t, struct test_t, 16)
+
+static bool array_test_init(void)
+{
+ AL_TEST_START("array_init");
+
+ array(u32, 64) arr;
+ al_array_init(u32, 64)(&arr);
+
+ AL_TEST_EQ((void *)arr.data, NULL, ptr);
+ AL_TEST_EQ(arr.alloc, 64, u32);
+ AL_TEST_EQ(arr.size, 0, u32);
+ AL_TEST_EQ(sizeof(arr.inline_data), 64 * sizeof(u32), size_t);
+
+ al_array_free(u32, 64)(&arr);
+
+ AL_TEST_END();
+}
+
+static bool array_test_reserve(void)
+{
+ AL_TEST_START("array_reserve");
+
+ array(u32, 64) arr;
+ al_array_init(u32, 64)(&arr);
+
+ al_array_reserve(u32, 64)(&arr, 64);
+
+ AL_TEST_EQ((void *)arr.data, NULL, ptr);
+ AL_TEST_EQ(arr.alloc, 64, u32);
+
+ al_array_reserve(u32, 64)(&arr, 128);
+
+ AL_TEST_NEQ((void *)arr.data, NULL, ptr);
+ AL_TEST_EQ(arr.alloc, 256, u32);
+
+ al_array_free(u32, 64)(&arr);
+
+ AL_TEST_END();
+}
+
+static bool array_test_at(void)
+{
+ AL_TEST_START("array_at");
+
+ array(u32, 64) arr;
+ al_array_init(u32, 64)(&arr);
+
+ u32 x = 5;
+ al_array_push(u32, 64)(&arr, &x);
+
+ AL_TEST_EQ(*al_array_at(u32, 64)(&arr, 0), 5, u32);
+
+ al_array_free(u32, 64)(&arr);
+
+ AL_TEST_END();
+}
+
+static bool array_test_last(void)
+{
+ AL_TEST_START("array_last");
+
+ array(u32, 64) arr;
+ al_array_init(u32, 64)(&arr);
+
+ u32 x = 1, y = 2, z = 3;
+
+ al_array_push(u32, 64)(&arr, &x);
+ al_array_push(u32, 64)(&arr, &y);
+ al_array_push(u32, 64)(&arr, &z);
+
+ AL_TEST_EQ(*al_array_last(u32, 64)(&arr), 3, u32);
+
+ al_array_free(u32, 64)(&arr);
+
+ AL_TEST_END();
+}
+
+static bool array_test_push(void)
+{
+ AL_TEST_START("array_push");
+
+ array(u32, 64) arr;
+ al_array_init(u32, 64)(&arr);
+
+ u32 x = 4, y = 5;
+
+ al_array_push(u32, 64)(&arr, &x);
+ al_array_push(u32, 64)(&arr, &y);
+
+ AL_TEST_EQ(arr.size, 2, u32);
+ AL_TEST_EQ(*al_array_at(u32, 64)(&arr, 0), x, u32);
+ AL_TEST_EQ(*al_array_at(u32, 64)(&arr, 1), y, u32);
+
+ al_array_free(u32, 64)(&arr);
+
+ AL_TEST_END();
+}
+
+static bool array_test_pop(void)
+{
+ AL_TEST_START("array_pop");
+
+ array(u32, 64) arr;
+ al_array_init(u32, 64)(&arr);
+
+ u32 x = 4, y = 5;
+
+ al_array_push(u32, 64)(&arr, &x);
+ al_array_push(u32, 64)(&arr, &y);
+
+ u32 rx, ry;
+
+ al_array_pop(u32, 64)(&arr, &ry);
+
+ AL_TEST_EQ(arr.size, 1, u32);
+
+ al_array_pop(u32, 64)(&arr, &rx);
+
+ AL_TEST_EQ(arr.size, 0, u32);
+ AL_TEST_EQ(rx, x, u32);
+ AL_TEST_EQ(ry, y, u32);
+
+ al_array_free(u32, 64)(&arr);
+
+ AL_TEST_END();
+}
+
+static bool array_test_remove_at(void)
+{
+ AL_TEST_START("array_remove_at");
+
+ array(u32) arr;
+ al_array_init(u32)(&arr);
+ al_array_reserve(u32)(&arr, 12);
+
+ u32 array_before[] = { 1, 2, 3, 4, 5, 6 };
+ u32 array_after[] = { 2, 3, 4 };
+
+ for (u32 i = 0; i < sizeof(array_before) / sizeof(u32); i++) {
+ al_array_push(u32)(&arr, &array_before[i]);
+ }
+
+ al_array_remove_at(u32)(&arr, 0);
+ al_array_remove_at(u32)(&arr, 3);
+ al_array_remove_at(u32)(&arr, 3);
+
+ AL_TEST_EQ(arr.size, (u32)(sizeof(array_after) / sizeof(u32)), u32);
+
+ for (u32 i = 0; i < sizeof(array_after) / sizeof(u32); i++) {
+ AL_TEST_EQ(*al_array_at(u32)(&arr, i), array_after[i], u32);
+ }
+
+ al_array_free(u32)(&arr);
+
+ AL_TEST_END();
+}
+
+static bool array_test_pop_at(void)
+{
+ AL_TEST_START("array_pop_at");
+
+ array(u32, 64) arr;
+ al_array_init(u32, 64)(&arr);
+
+ u32 array_before[] = { 1, 2, 3, 4, 5, 6 };
+ u32 array_after[] = { 2, 3, 4 };
+
+ for (u32 i = 0; i < sizeof(array_before) / sizeof(u32); i++) {
+ al_array_push(u32, 64)(&arr, &array_before[i]);
+ }
+
+ u32 x, y, z;
+
+ al_array_pop_at(u32, 64)(&arr, 0, &x);
+ al_array_pop_at(u32, 64)(&arr, 3, &y);
+ al_array_pop_at(u32, 64)(&arr, 3, &z);
+
+ AL_TEST_EQ(x, array_before[0], u32);
+ AL_TEST_EQ(y, array_before[4], u32);
+ AL_TEST_EQ(z, array_before[5], u32);
+
+ AL_TEST_EQ(arr.size, (u32)(sizeof(array_after) / sizeof(u32)), u32);
+
+ for (u32 i = 0; i < sizeof(array_after) / sizeof(u32); i++) {
+ AL_TEST_EQ(*al_array_at(u32, 64)(&arr, i), array_after[i], u32);
+ }
+
+ al_array_free(u32, 64)(&arr);
+
+ AL_TEST_END();
+}
+
+static bool array_test_insert(void)
+{
+ AL_TEST_START("array_insert");
+
+ array(test_t, 16) arr;
+ al_array_init(test_t, 16)(&arr);
+
+ struct test_t x = {
+ .i = 5
+ };
+
+ struct test_t y = {
+ .i = 5
+ };
+
+ struct test_t z = {
+ .i = 5
+ };
+
+ al_array_insert(test_t, 16)(&arr, &x, 1);
+ al_array_insert(test_t, 16)(&arr, &y, 0);
+ al_array_insert(test_t, 16)(&arr, &z, 1);
+
+ AL_TEST_EQ(al_array_at(test_t, 16)(&arr, 3)->i, x.i, u32);
+ AL_TEST_EQ(al_array_at(test_t, 16)(&arr, 0)->i, y.i, u32);
+ AL_TEST_EQ(al_array_at(test_t, 16)(&arr, 1)->i, z.i, u32);
+
+ AL_TEST_EQ(arr.size, 4, u32);
+
+ al_array_remove_at(test_t, 16)(&arr, 3);
+
+ AL_TEST_EQ(arr.size, 3, u32);
+
+ AL_TEST_EQ(al_array_at(test_t, 16)(&arr, 0)->i, y.i, u32);
+ AL_TEST_EQ(al_array_at(test_t, 16)(&arr, 1)->i, z.i, u32);
+
+ al_array_free(test_t, 16)(&arr);
+
+ AL_TEST_END();
+}
+
+bool array_typed_tests_run(void)
+{
+ AL_TEST_START_GROUP("array_typed");
+
+ AL_TEST_RUN(array_test_init);
+ AL_TEST_RUN(array_test_reserve);
+ AL_TEST_RUN(array_test_at);
+ AL_TEST_RUN(array_test_last);
+ AL_TEST_RUN(array_test_push);
+ AL_TEST_RUN(array_test_pop);
+ AL_TEST_RUN(array_test_remove_at);
+ AL_TEST_RUN(array_test_pop_at);
+ AL_TEST_RUN(array_test_insert);
+
+ AL_TEST_END_GROUP();
+}
diff --git a/tests/array_typed.h b/tests/array_typed.h
new file mode 100644
index 0000000..644cce6
--- /dev/null
+++ b/tests/array_typed.h
@@ -0,0 +1,8 @@
+#ifndef _AL_ARRAY_TYPED_TESTS_H
+#define _AL_ARRAY_TYPED_TESTS_H
+
+#include <al/types.h>
+
+bool array_typed_tests_run(void);
+
+#endif // _AL_ARRAY_TYPED_TESTS_H
diff --git a/tests/main.c b/tests/main.c
new file mode 100644
index 0000000..622e969
--- /dev/null
+++ b/tests/main.c
@@ -0,0 +1,23 @@
+#include <al/lib.h>
+
+#include "array.h"
+#ifdef HAVE_GNU_EXTENSIONS
+#include "array_typed.h"
+#endif
+#include "str.h"
+#include "ring_buffer.h"
+
+s32 main(void)
+{
+ if (!array_tests_run()) goto fail;
+#ifdef HAVE_GNU_EXTENSIONS
+ if (!array_typed_tests_run()) goto fail;
+#endif
+ if (!str_tests_run()) goto fail;
+ if (!ring_buffer_tests_run()) goto fail;
+
+ return EXIT_SUCCESS;
+
+fail:
+ return EXIT_FAILURE;
+}
diff --git a/tests/ring_buffer.c b/tests/ring_buffer.c
new file mode 100644
index 0000000..3ed7d4d
--- /dev/null
+++ b/tests/ring_buffer.c
@@ -0,0 +1,198 @@
+#include <al/test.h>
+#include <al/ring_buffer.h>
+
+#define TEST_BUFFER_SIZE 32
+
+static u8 buffer[TEST_BUFFER_SIZE];
+static u8 buffer_ret[TEST_BUFFER_SIZE];
+static u8 buffer_half[TEST_BUFFER_SIZE / 2] = {0};
+static u8 buffer_over[TEST_BUFFER_SIZE + 16] = {0};
+
+static bool ring_buffer_test_init(void)
+{
+ AL_TEST_START("ring_buffer_init");
+
+ struct al_ring_buffer buf;
+ al_ring_buffer_init(&buf, buffer, TEST_BUFFER_SIZE);
+
+ AL_TEST_EQ(buf.start, buffer, ptr);
+ AL_TEST_EQ(buf.end, buffer + TEST_BUFFER_SIZE, ptr);
+ AL_TEST_EQ(al_atomic_ptr_load(&buf.read, AL_ATOMIC_RELAXED), buffer, ptr);
+ AL_TEST_EQ(al_atomic_ptr_load(&buf.write, AL_ATOMIC_RELAXED), buffer, ptr);
+
+ AL_TEST_END();
+}
+
+static bool ring_buffer_test_space(void)
+{
+ AL_TEST_START("ring_buffer_space");
+
+ struct al_ring_buffer buf;
+ al_ring_buffer_init(&buf, buffer, TEST_BUFFER_SIZE);
+
+ AL_TEST_EQ(al_ring_buffer_space(&buf), TEST_BUFFER_SIZE - 1, size_t);
+
+ AL_TEST_END();
+}
+
+static bool ring_buffer_test_occupied(void)
+{
+ AL_TEST_START("ring_buffer_occupied");
+
+ struct al_ring_buffer buf;
+ al_ring_buffer_init(&buf, buffer, TEST_BUFFER_SIZE);
+
+ AL_TEST_EQ(al_ring_buffer_occupied(&buf), 0, size_t);
+
+ AL_TEST_END();
+}
+
+static bool ring_buffer_test_read_and_write_chunk(void)
+{
+ AL_TEST_START("ring_buffer_read_and_write_chunk");
+
+ struct al_ring_buffer buf;
+ al_ring_buffer_init(&buf, buffer, TEST_BUFFER_SIZE);
+
+ char *data = "TestTEST";
+ size_t data_size = al_strlen(data) + 1;
+
+ size_t size;
+ u8 *ptr = al_ring_buffer_write_chunk(&buf, &size);
+
+ AL_TEST_EQ(size, TEST_BUFFER_SIZE - 1, size_t);
+
+ al_memcpy(ptr, data, data_size);
+ al_ring_buffer_append(&buf, ptr, data_size);
+
+ AL_TEST_EQ(al_ring_buffer_occupied(&buf), data_size, size_t);
+ AL_TEST_EQ(al_ring_buffer_space(&buf), (TEST_BUFFER_SIZE - 1) - data_size, size_t);
+
+ ptr = al_ring_buffer_read_chunk(&buf, &size);
+ AL_TEST_EQ(size, data_size, size_t);
+ AL_TEST_TRUE((strcmp((char *)ptr, data) == 0));
+ al_ring_buffer_consume(&buf, ptr, size);
+
+ AL_TEST_EQ(al_ring_buffer_occupied(&buf), 0, size_t);
+ AL_TEST_EQ(al_ring_buffer_space(&buf), TEST_BUFFER_SIZE - 1, size_t);
+
+ AL_TEST_END();
+}
+
+static bool ring_buffer_test_read_and_write(void)
+{
+ AL_TEST_START("ring_buffer_read_and_write");
+
+ struct al_ring_buffer buf;
+ al_ring_buffer_init(&buf, buffer, TEST_BUFFER_SIZE);
+
+ char *data = "TestTEST";
+ size_t data_size = al_strlen(data) + 1;
+
+ al_ring_buffer_write(&buf, (u8 *)data, data_size);
+
+ AL_TEST_EQ(al_ring_buffer_occupied(&buf), data_size, size_t);
+ AL_TEST_EQ(al_ring_buffer_space(&buf), (TEST_BUFFER_SIZE - 1) - data_size, size_t);
+
+ al_ring_buffer_read(&buf, (u8 *)buffer_ret, data_size);
+ AL_TEST_TRUE((strcmp((char *)buffer_ret, data) == 0));
+
+ AL_TEST_EQ(al_ring_buffer_occupied(&buf), 0, size_t);
+ AL_TEST_EQ(al_ring_buffer_space(&buf), TEST_BUFFER_SIZE - 1, size_t);
+
+ al_memset(buffer_half, 'd', sizeof(buffer_half));
+
+ AL_TEST_EQ(al_ring_buffer_write(&buf, buffer_half, sizeof(buffer_half)), sizeof(buffer_half), size_t);
+ AL_TEST_EQ(al_ring_buffer_occupied(&buf), sizeof(buffer_half), size_t);
+ AL_TEST_EQ(al_ring_buffer_space(&buf), (TEST_BUFFER_SIZE - 1) - sizeof(buffer_half), size_t);
+
+ AL_TEST_EQ(al_ring_buffer_read(&buf, (u8 *)buffer_ret, data_size), data_size, size_t);
+ AL_TEST_TRUE(al_memcmp(buffer_ret, "ddddddddd", data_size) == 0);
+
+ AL_TEST_EQ(al_ring_buffer_write(&buf, buffer_half, sizeof(buffer_half)), sizeof(buffer_half), size_t);
+ AL_TEST_EQ(al_ring_buffer_occupied(&buf), TEST_BUFFER_SIZE - data_size, size_t);
+ AL_TEST_LT(al_atomic_ptr_load(&buf.write, AL_ATOMIC_RELAXED), al_atomic_ptr_load(&buf.read, AL_ATOMIC_RELAXED), ptr);
+
+ size_t space = al_ring_buffer_space(&buf);
+ AL_TEST_EQ(space, data_size - 1, size_t);
+ AL_TEST_EQ(al_ring_buffer_write(&buf, buffer_over, sizeof(buffer_over)), space, size_t);
+
+ AL_TEST_EQ(al_ring_buffer_read(&buf, buffer_ret, sizeof(buffer_over)), TEST_BUFFER_SIZE - 1, size_t);
+
+ AL_TEST_END();
+}
+
+static bool ring_buffer_test_peek(void)
+{
+ AL_TEST_START("ring_buffer_peek");
+
+ struct al_ring_buffer buf;
+ al_ring_buffer_init(&buf, buffer, TEST_BUFFER_SIZE);
+
+ char *data = "abcabcabcabcabcabcabcabcabcabca";
+ size_t data_size = al_strlen(data);
+ AL_TEST_EQ(data_size, TEST_BUFFER_SIZE - 1, size_t);
+
+ al_ring_buffer_write(&buf, (u8 *)data, data_size);
+
+ al_ring_buffer_peek(&buf, buffer_ret, 0, data_size);
+ AL_TEST_TRUE(al_memcmp(buffer_ret, data, data_size) == 0);
+
+ al_ring_buffer_peek(&buf, buffer_ret, 3, data_size);
+ AL_TEST_TRUE(al_memcmp(buffer_ret, data + 3, data_size - 3) == 0);
+
+ al_ring_buffer_read(&buf, buffer_ret, 3);
+ AL_TEST_TRUE(al_memcmp(buffer_ret, data, 3) == 0);
+ al_ring_buffer_write(&buf, (u8 *)data, data_size);
+
+ al_ring_buffer_peek(&buf, buffer_ret, 3, data_size);
+ AL_TEST_TRUE(al_memcmp(buffer_ret, data + 6, data_size - 6) == 0);
+
+ al_ring_buffer_peek(&buf, buffer_ret, 0, data_size);
+ AL_TEST_TRUE(al_memcmp(buffer_ret, "abcabcabcabcabcabcabcabcabcaabc", data_size) == 0);
+
+ al_ring_buffer_read(&buf, buffer_ret, (TEST_BUFFER_SIZE - 1) - 4);
+
+ al_ring_buffer_peek(&buf, buffer_ret, 0, data_size);
+ AL_TEST_TRUE(al_memcmp(buffer_ret, "aab", 3) == 0);
+
+ AL_TEST_EQ(al_ring_buffer_peek(&buf, buffer_ret, 10, data_size), 0, size_t);
+ AL_TEST_EQ(al_ring_buffer_peek(&buf, buffer_ret, 3, data_size), 1, size_t);
+ AL_TEST_EQ(al_ring_buffer_peek(&buf, buffer_ret, 4, data_size), 0, size_t);
+
+ al_ring_buffer_peek(&buf, buffer_ret, 1, data_size);
+ AL_TEST_TRUE(al_memcmp(buffer_ret, "abc", 3) == 0);
+
+ AL_TEST_EQ(al_ring_buffer_peek(&buf, buffer_ret, 2, data_size), 2, size_t);
+ AL_TEST_TRUE(al_memcmp(buffer_ret, "bc", 2) == 0);
+
+ al_ring_buffer_read(&buf, buffer_ret, 1);
+
+ AL_TEST_EQ(al_ring_buffer_peek(&buf, buffer_ret, 0, data_size), 3, size_t);
+ AL_TEST_TRUE(al_memcmp(buffer_ret, "abc", 3) == 0);
+
+ AL_TEST_EQ(al_ring_buffer_peek(&buf, buffer_ret, 3, data_size), 0, size_t);
+ AL_TEST_EQ(al_ring_buffer_peek(&buf, buffer_ret, 4, data_size), 0, size_t);
+
+ AL_TEST_EQ(al_ring_buffer_read(&buf, buffer_ret, 4), 3, size_t);
+
+ AL_TEST_EQ(al_ring_buffer_peek(&buf, buffer_ret, 0, data_size), 0, size_t);
+ AL_TEST_EQ(al_ring_buffer_peek(&buf, buffer_ret, 1, data_size), 0, size_t);
+ AL_TEST_EQ(al_ring_buffer_peek(&buf, buffer_ret, 2, data_size), 0, size_t);
+
+ AL_TEST_END();
+}
+
+bool ring_buffer_tests_run(void)
+{
+ AL_TEST_START_GROUP("ring_buffer");
+
+ AL_TEST_RUN(ring_buffer_test_init);
+ AL_TEST_RUN(ring_buffer_test_space);
+ AL_TEST_RUN(ring_buffer_test_occupied);
+ AL_TEST_RUN(ring_buffer_test_read_and_write_chunk);
+ AL_TEST_RUN(ring_buffer_test_read_and_write);
+ AL_TEST_RUN(ring_buffer_test_peek);
+
+ AL_TEST_END_GROUP();
+}
diff --git a/tests/ring_buffer.h b/tests/ring_buffer.h
new file mode 100644
index 0000000..920c32d
--- /dev/null
+++ b/tests/ring_buffer.h
@@ -0,0 +1,8 @@
+#ifndef _AL_RING_BUFFER_TESTS_H
+#define _AL_RING_BUFFER_TESTS_H
+
+#include <al/types.h>
+
+bool ring_buffer_tests_run(void);
+
+#endif // _AL_RING_BUFFER_TESTS_H
diff --git a/tests/str.c b/tests/str.c
new file mode 100644
index 0000000..0dcd896
--- /dev/null
+++ b/tests/str.c
@@ -0,0 +1,191 @@
+#include <al/test.h>
+#include <al/str.h>
+
+static bool str_test_cmp(void)
+{
+ AL_TEST_START("str_cmp");
+
+ str *s0 = al_str_c("testtestasdf");
+ str *s1 = al_str_c("testtestasdf");
+ str *s2 = al_str_c("testtedfdfstasdf");
+ str *s3 = al_str_c("dtesttedfdfstasdf");
+
+ AL_TEST_TRUE(al_str_eq(s0, s1));
+ AL_TEST_FALSE(al_str_eq(s0, s2));
+ AL_TEST_FALSE(al_str_eq(s0, s3));
+ AL_TEST_FALSE(al_str_eq(s2, s3));
+
+ AL_TEST_EQ(al_str_cmp(s0, al_str_c("test"), 0, 4), 0, u32);
+ AL_TEST_EQ(al_str_cmp(s0, al_str_c("tt"), 3, 2), 0, u32);
+ AL_TEST_GT(al_str_cmp(s0, al_str_c("r"), 6, 1), 0, u32);
+ AL_TEST_EQ(al_str_cmp(s0, al_str_c("ee"), 15, 2), -1, u32);
+
+ str *s4 = al_str_c("page");
+ str *s5 = al_str_c("page.json");
+
+ AL_TEST_FALSE(al_str_eq(s5, s4));
+ AL_TEST_FALSE(al_str_eq(s4, s5));
+
+ AL_TEST_END();
+}
+
+static bool str_test_cat(void)
+{
+ AL_TEST_START("str_cat");
+
+ str *result = al_str_c("testtestasdf");
+
+ str s0;
+ al_str_from(&s0, "");
+
+ AL_TEST_EQ(s0.len, 0, u32);
+
+ al_str_cat(&s0, al_str_c("testtestasdf"));
+
+ AL_TEST_TRUE(al_str_eq(result, &s0));
+
+ al_str_free(&s0);
+
+ AL_TEST_END();
+}
+
+static bool str_test_insert(void)
+{
+ AL_TEST_START("str_insert");
+
+ str *insert_result = al_str_c("testtest[insert]asdf");
+ str *prepend_result = al_str_c("[prepend]testtestasdf");
+
+ str insert_str;
+ str prepend_str;
+
+ al_str_from(&insert_str, "testtestasdf");
+ al_str_from(&prepend_str, "testtestasdf");
+
+ al_str_insert(&insert_str, al_str_c("[insert]"), 8);
+ al_str_prepend(&prepend_str, al_str_c("[prepend]"));
+
+ AL_TEST_TRUE(al_str_eq(insert_result, &insert_str));
+ AL_TEST_TRUE(al_str_eq(prepend_result, &prepend_str));
+
+ al_str_free(&insert_str);
+ al_str_free(&prepend_str);
+
+ AL_TEST_END();
+}
+
+static bool str_test_find_rfind(void)
+{
+ AL_TEST_START("str_find_rfind");
+
+ str *s0 = al_str_c("testtes\\ta\\sdf");
+
+ AL_TEST_EQ(al_str_find(s0, '\\'), 7, s32);
+ AL_TEST_EQ(al_str_rfind(s0, '\\'), 10, s32);
+
+ AL_TEST_EQ(al_str_find_str(s0, al_str_c("tes\\")), 4, s32);
+
+ AL_TEST_END();
+}
+
+static bool str_test_substr(void)
+{
+ AL_TEST_START("str_substr");
+
+ str *result0 = al_str_c("[cutout0]");
+ str *result1 = al_str_c("[cutout1]");
+
+ str s;
+ al_str_from(&s, "dfkdljfd[cutout0]dfd[cutout1]fd");
+
+ s32 start = al_str_find(&s, '[');
+ s32 end = al_str_find(&s, ']') + 1;
+
+ str *cutout0 = al_str_substr(&s, start, end);
+ str *sub = al_str_substr(&s, end, s.len);
+
+ start = al_str_find(sub, '[');
+ end = al_str_find(sub, ']') + 1;
+
+ str *cutout1 = al_str_substr(sub, start, end);
+
+ AL_TEST_TRUE(al_str_eq(result0, cutout0));
+ AL_TEST_TRUE(al_str_eq(result1, cutout1));
+
+ al_str_free(&s);
+
+ AL_TEST_END();
+}
+
+static bool str_test_get_line(void)
+{
+ AL_TEST_START("str_get_line");
+
+ str *lines = al_str_c("test\nasdf\ntest2\na\n");
+ str *lines_array[] = { al_str_c("test"), al_str_c("asdf"), al_str_c("test2"), al_str_c("a") };
+
+ u32 count = 0;
+
+ for (str line = { 0 }; al_str_get_line(lines, &line); ) {
+ AL_TEST_TRUE(al_str_eq(&line, lines_array[count++]));
+ }
+
+ AL_TEST_END();
+}
+
+static bool str_test_to_long(void)
+{
+ AL_TEST_START("str_to_long");
+
+ str *s0 = al_str_c("10353");
+ str *s1 = al_str_c("-3563");
+ str *s2 = al_str_c("-2147483647"); // 1 away
+ // This fails on MSVC for some reason.
+ str *s2_1 = al_str_c("-2147483648"); // min
+ str *s2_2 = al_str_c("-2147483649"); // underflow
+ str *s3 = al_str_c("0xfff");
+ str *s4 = al_str_c("0b01010");
+ str *s5 = al_str_c("01010");
+ str *s6 = al_str_c("99999999999999999999");
+ str *s7 = al_str_c(" -99999999999999999999");
+ str *s8 = al_str_c("---99999999999999999999");
+ str *s9 = al_str_c("'p[afd'dfdfs[f");
+ str *s10 = al_str_c("\n\t 01010");
+ str *s11 = al_str_c("-9223372036854775809");
+ str *s12 = al_str_c("6519749124957310923");
+ str *s13 = al_str_c("-3519311901742109472");
+
+ AL_TEST_EQ(al_str_to_long(s0, 10), 10353L, s64);
+ AL_TEST_EQ(al_str_to_long(s1, 10), -3563L, s64);
+ AL_TEST_EQ(al_str_to_long(s2, 10), -2147483647L, s64);
+ AL_TEST_EQ(al_str_to_long(s2_1, 10), -2147483648L, s64);
+ AL_TEST_EQ(al_str_to_long(s2_2, 10), -2147483649L, s64);
+ AL_TEST_EQ(al_str_to_long(s3, 16), 0xfff, s64);
+ AL_TEST_EQ(al_str_to_long(s4, 2), 0b01010, s64);
+ AL_TEST_EQ(al_str_to_long(s5, 2), 0b01010, s64);
+ AL_TEST_EQ(al_str_to_long(s6, 10), INT64_MAX, s64);
+ AL_TEST_EQ(al_str_to_long(s7, 10), INT64_MIN, s64);
+ AL_TEST_EQ(al_str_to_long(s8, 10), INT64_MIN, s64);
+ AL_TEST_EQ(al_str_to_long(s9, 10), INT64_MAX, s64);
+ AL_TEST_EQ(al_str_to_long(s10, 2), 0b01010, s64);
+ AL_TEST_EQ(al_str_to_long(s11, 10), INT64_MIN, s64);
+ AL_TEST_EQ(al_str_to_long(s12, 10), 6519749124957310923L, s64);
+ AL_TEST_EQ(al_str_to_long(s13, 10), -3519311901742109472, s64);
+
+ AL_TEST_END();
+}
+
+bool str_tests_run(void)
+{
+ AL_TEST_START_GROUP("str");
+
+ AL_TEST_RUN(str_test_cmp);
+ AL_TEST_RUN(str_test_cat);
+ AL_TEST_RUN(str_test_insert);
+ AL_TEST_RUN(str_test_find_rfind);
+ AL_TEST_RUN(str_test_substr);
+ AL_TEST_RUN(str_test_get_line);
+ AL_TEST_RUN(str_test_to_long);
+
+ AL_TEST_END_GROUP();
+}
diff --git a/tests/str.h b/tests/str.h
new file mode 100644
index 0000000..64eb0fa
--- /dev/null
+++ b/tests/str.h
@@ -0,0 +1,8 @@
+#ifndef _AL_STR_TESTS_H
+#define _AL_STR_TESTS_H
+
+#include <al/types.h>
+
+bool str_tests_run(void);
+
+#endif // _AL_STR_TESTS_H