xref: /aosp_15_r20/external/pigweed/pw_allocator/BUILD.bazel (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1# Copyright 2020 The Pigweed Authors
2#
3# Licensed under the Apache License, Version 2.0 (the "License"); you may not
4# use this file except in compliance with the License. You may obtain a copy of
5# the License at
6#
7#     https://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12# License for the specific language governing permissions and limitations under
13# the License.
14
15load("//pw_unit_test:pw_cc_test.bzl", "pw_cc_test")
16
17package(default_visibility = ["//visibility:public"])
18
19licenses(["notice"])
20
21# Module configuration
22
23cc_library(
24    name = "config",
25    hdrs = ["public/pw_allocator/config.h"],
26    strip_include_prefix = "public",
27    deps = [":config_override"],
28)
29
30label_flag(
31    name = "config_override",
32    build_setting_default = "//pw_build:default_module_config",
33)
34
35cc_library(
36    name = "test_config",
37    defines = [
38        "PW_ALLOCATOR_STRICT_VALIDATION=1",
39        "PW_ALLOCATOR_BLOCK_POISON_INTERVAL=4",
40    ],
41)
42
43# Libraries
44
45cc_library(
46    name = "allocator",
47    srcs = [
48        "allocator.cc",
49    ],
50    hdrs = [
51        "public/pw_allocator/allocator.h",
52    ],
53    strip_include_prefix = "public",
54    deps = [
55        ":deallocator",
56        "//pw_result",
57    ],
58)
59
60cc_library(
61    name = "allocator_as_pool",
62    srcs = [
63        "allocator_as_pool.cc",
64    ],
65    hdrs = [
66        "public/pw_allocator/allocator_as_pool.h",
67    ],
68    strip_include_prefix = "public",
69    deps = [
70        ":allocator",
71        ":pool",
72        "//pw_status",
73    ],
74)
75
76cc_library(
77    name = "best_fit",
78    hdrs = ["public/pw_allocator/best_fit.h"],
79    strip_include_prefix = "public",
80    deps = [
81        ":block_allocator",
82        ":config",
83        "//pw_allocator/block:detailed_block",
84        "//pw_allocator/bucket:fast_sorted",
85        "//pw_allocator/bucket:sorted",
86    ],
87)
88
89cc_library(
90    name = "best_fit_block_allocator",
91    hdrs = ["public/pw_allocator/best_fit_block_allocator.h"],
92    includes = ["public"],
93    deps = [
94        ":best_fit",
95        ":config",
96    ],
97)
98
99cc_library(
100    name = "block_allocator",
101    srcs = ["block_allocator.cc"],
102    hdrs = ["public/pw_allocator/block_allocator.h"],
103    strip_include_prefix = "public",
104    deps = [
105        ":allocator",
106        ":fragmentation",
107        "//pw_allocator/block:allocatable",
108        "//pw_allocator/block:basic",
109        "//pw_allocator/block:iterable",
110        "//pw_allocator/block:poisonable",
111        "//pw_allocator/block:with_layout",
112        "//pw_assert",
113        "//pw_bytes:alignment",
114        "//pw_result",
115        "//pw_status",
116    ],
117)
118
119cc_library(
120    name = "bucket_allocator",
121    hdrs = [
122        "public/pw_allocator/bucket_allocator.h",
123    ],
124    strip_include_prefix = "public",
125    deps = [
126        ":block_allocator",
127        "//pw_allocator/block:detailed_block",
128        "//pw_allocator/bucket:unordered",
129        "//pw_status",
130    ],
131)
132
133# TODO(b/376730645): Remove deprecated interfaces.
134cc_library(
135    name = "bucket_block_allocator",
136    hdrs = ["public/pw_allocator/bucket_block_allocator.h"],
137    strip_include_prefix = "public",
138    deps = [":bucket_allocator"],
139)
140
141cc_library(
142    name = "buddy_allocator",
143    srcs = [
144        "buddy_allocator.cc",
145    ],
146    hdrs = [
147        "public/pw_allocator/buddy_allocator.h",
148    ],
149    implementation_deps = [
150        "//pw_assert",
151        "//pw_bytes:alignment",
152        "//third_party/fuchsia:stdcompat",
153    ],
154    strip_include_prefix = "public",
155    deps = [
156        ":allocator",
157        "//pw_allocator/block:basic",
158        "//pw_allocator/bucket:unordered",
159        "//pw_bytes",
160        "//pw_containers:vector",
161        "//pw_status",
162    ],
163)
164
165cc_library(
166    name = "buffer",
167    hdrs = [
168        "public/pw_allocator/buffer.h",
169    ],
170    strip_include_prefix = "public",
171    deps = [
172        "//pw_assert",
173        "//pw_bytes",
174        "//pw_bytes:alignment",
175        "//pw_result",
176    ],
177)
178
179cc_library(
180    name = "bump_allocator",
181    srcs = [
182        "bump_allocator.cc",
183    ],
184    hdrs = [
185        "public/pw_allocator/bump_allocator.h",
186    ],
187    strip_include_prefix = "public",
188    deps = [
189        ":allocator",
190        ":buffer",
191        "//pw_bytes",
192        "//pw_bytes:alignment",
193    ],
194)
195
196cc_library(
197    name = "chunk_pool",
198    srcs = [
199        "chunk_pool.cc",
200    ],
201    hdrs = [
202        "public/pw_allocator/chunk_pool.h",
203    ],
204    implementation_deps = [
205        ":buffer",
206        "//pw_assert",
207        "//pw_bytes:alignment",
208        "//third_party/fuchsia:stdcompat",
209    ],
210    strip_include_prefix = "public",
211    deps = [
212        ":pool",
213        "//pw_bytes",
214        "//pw_result",
215    ],
216)
217
218cc_library(
219    name = "deallocator",
220    srcs = [
221        "unique_ptr.cc",
222    ],
223    hdrs = [
224        "public/pw_allocator/capability.h",
225        "public/pw_allocator/deallocator.h",
226        "public/pw_allocator/layout.h",
227        "public/pw_allocator/unique_ptr.h",
228    ],
229    strip_include_prefix = "public",
230    deps = [
231        "//pw_assert",
232        "//pw_preprocessor",
233        "//pw_result",
234        "//pw_status",
235    ],
236)
237
238# TODO(b/376730645): Remove deprecated interfaces.
239cc_library(
240    name = "dual_first_fit_block_allocator",
241    hdrs = ["public/pw_allocator/dual_first_fit_block_allocator.h"],
242    strip_include_prefix = "public",
243    deps = [":first_fit"],
244)
245
246cc_library(
247    name = "fallback_allocator",
248    srcs = [
249        "fallback_allocator.cc",
250    ],
251    hdrs = [
252        "public/pw_allocator/fallback_allocator.h",
253    ],
254    strip_include_prefix = "public",
255    deps = [
256        ":allocator",
257        ":deallocator",
258        "//pw_assert",
259        "//pw_result",
260        "//pw_status",
261    ],
262)
263
264cc_library(
265    name = "first_fit",
266    hdrs = ["public/pw_allocator/first_fit.h"],
267    strip_include_prefix = "public",
268    deps = [
269        ":block_allocator",
270        ":config",
271        "//pw_allocator/block:detailed_block",
272        "//pw_allocator/bucket:sequenced",
273    ],
274)
275
276# TODO(b/376730645): Remove deprecated interfaces.
277cc_library(
278    name = "first_fit_block_allocator",
279    hdrs = ["public/pw_allocator/first_fit_block_allocator.h"],
280    includes = ["public"],
281    deps = [":first_fit"],
282)
283
284cc_library(
285    name = "fragmentation",
286    srcs = ["fragmentation.cc"],
287    hdrs = ["public/pw_allocator/fragmentation.h"],
288    strip_include_prefix = "public",
289)
290
291cc_library(
292    name = "freelist_heap",
293    hdrs = [
294        "public/pw_allocator/freelist_heap.h",
295    ],
296    strip_include_prefix = "public",
297    deps = [
298        ":bucket_allocator",
299        "//pw_assert",
300        "//pw_bytes",
301        "//pw_preprocessor",
302    ],
303)
304
305# TODO(b/376730645): Remove deprecated interfaces.
306cc_library(
307    name = "last_fit_block_allocator",
308    hdrs = ["public/pw_allocator/last_fit_block_allocator.h"],
309    strip_include_prefix = "public",
310    deps = [":first_fit"],
311)
312
313cc_library(
314    name = "libc_allocator",
315    srcs = [
316        "libc_allocator.cc",
317    ],
318    hdrs = [
319        "public/pw_allocator/libc_allocator.h",
320    ],
321    strip_include_prefix = "public",
322    deps = [
323        ":allocator",
324    ],
325)
326
327cc_library(
328    name = "null_allocator",
329    srcs = [
330        "null_allocator.cc",
331    ],
332    hdrs = [
333        "public/pw_allocator/null_allocator.h",
334    ],
335    strip_include_prefix = "public",
336    deps = [
337        ":allocator",
338    ],
339)
340
341cc_library(
342    name = "pmr_allocator",
343    srcs = ["pmr_allocator.cc"],
344    hdrs = ["public/pw_allocator/pmr_allocator.h"],
345    strip_include_prefix = "public",
346    deps = [
347        ":allocator",
348        ":config",
349    ],
350)
351
352cc_library(
353    name = "pool",
354    hdrs = ["public/pw_allocator/pool.h"],
355    strip_include_prefix = "public",
356    deps = [
357        ":deallocator",
358        "//pw_bytes",
359        "//pw_result",
360    ],
361)
362
363cc_library(
364    name = "synchronized_allocator",
365    hdrs = [
366        "public/pw_allocator/synchronized_allocator.h",
367    ],
368    strip_include_prefix = "public",
369    deps = [
370        ":allocator",
371        "//pw_sync:borrow",
372    ],
373)
374
375cc_library(
376    name = "tracking_allocator",
377    hdrs = [
378        "public/pw_allocator/metrics.h",
379        "public/pw_allocator/tracking_allocator.h",
380    ],
381    strip_include_prefix = "public",
382    deps = [
383        ":allocator",
384        "//pw_assert",
385        "//pw_metric:metric",
386        "//pw_status",
387    ],
388)
389
390cc_library(
391    name = "typed_pool",
392    hdrs = [
393        "public/pw_allocator/typed_pool.h",
394    ],
395    strip_include_prefix = "public",
396    deps = [
397        ":allocator",
398        ":chunk_pool",
399        "//pw_bytes",
400        "//pw_result",
401    ],
402)
403
404cc_library(
405    name = "worst_fit",
406    hdrs = ["public/pw_allocator/worst_fit.h"],
407    strip_include_prefix = "public",
408    deps = [
409        ":block_allocator",
410        ":config",
411        "//pw_allocator/block:detailed_block",
412        "//pw_allocator/bucket:fast_sorted",
413        "//pw_allocator/bucket:sorted",
414    ],
415)
416
417cc_library(
418    name = "worst_fit_block_allocator",
419    hdrs = ["public/pw_allocator/worst_fit_block_allocator.h"],
420    includes = ["public"],
421    deps = [
422        ":config",
423        ":worst_fit",
424    ],
425)
426
427# Test support
428
429cc_library(
430    name = "testing",
431    testonly = True,
432    hdrs = [
433        "public/pw_allocator/testing.h",
434    ],
435    strip_include_prefix = "public",
436    deps = [
437        ":allocator",
438        ":buffer",
439        ":first_fit",
440        ":test_config",
441        ":tracking_allocator",
442        "//pw_assert",
443        "//pw_bytes",
444        "//pw_result",
445        "//pw_status",
446        "//pw_sync:interrupt_spin_lock",
447        "//pw_unit_test",
448    ],
449)
450
451cc_library(
452    name = "block_allocator_testing",
453    testonly = True,
454    srcs = [
455        "block_allocator_testing.cc",
456    ],
457    hdrs = [
458        "public/pw_allocator/block_allocator_testing.h",
459    ],
460    implementation_deps = [
461        "//pw_assert",
462        "//pw_bytes:alignment",
463        "//pw_status",
464        "//third_party/fuchsia:stdcompat",
465    ],
466    strip_include_prefix = "public",
467    deps = [
468        ":block_allocator",
469        "//pw_allocator/block:testing",
470        "//pw_unit_test",
471    ],
472)
473
474cc_library(
475    name = "test_harness",
476    testonly = True,
477    srcs = [
478        "test_harness.cc",
479    ],
480    hdrs = [
481        "public/pw_allocator/test_harness.h",
482    ],
483    strip_include_prefix = "public",
484    deps = [
485        ":allocator",
486        "//pw_assert",
487        "//pw_containers",
488        "//pw_random",
489        "//third_party/fuchsia:stdcompat",
490    ],
491)
492
493cc_library(
494    name = "fuzzing",
495    testonly = True,
496    srcs = [
497        "fuzzing.cc",
498    ],
499    hdrs = [
500        "public/pw_allocator/fuzzing.h",
501    ],
502    strip_include_prefix = "public",
503    deps = [
504        ":test_harness",
505        "//pw_fuzzer:fuzztest",
506    ],
507)
508
509# Tests
510
511pw_cc_test(
512    name = "allocator_as_pool_test",
513    srcs = [
514        "allocator_as_pool_test.cc",
515    ],
516    deps = [
517        ":allocator_as_pool",
518        ":testing",
519        "//pw_unit_test",
520    ],
521)
522
523pw_cc_test(
524    name = "allocator_test",
525    srcs = [
526        "allocator_test.cc",
527    ],
528    deps = [
529        ":allocator",
530        ":testing",
531        "//pw_unit_test",
532    ],
533)
534
535pw_cc_test(
536    name = "best_fit_test",
537    srcs = ["best_fit_test.cc"],
538    deps = [
539        ":best_fit",
540        ":best_fit_block_allocator",
541        ":block_allocator_testing",
542        "//pw_unit_test",
543    ],
544)
545
546pw_cc_test(
547    name = "bucket_allocator_test",
548    srcs = ["bucket_allocator_test.cc"],
549    deps = [
550        ":block_allocator_testing",
551        ":bucket_allocator",
552        ":bucket_block_allocator",
553        "//pw_unit_test",
554    ],
555)
556
557pw_cc_test(
558    name = "buddy_allocator_test",
559    srcs = [
560        "buddy_allocator_test.cc",
561    ],
562    deps = [
563        ":buddy_allocator",
564        ":testing",
565        "//pw_unit_test",
566    ],
567)
568
569pw_cc_test(
570    name = "buffer_test",
571    srcs = [
572        "buffer_test.cc",
573    ],
574    deps = [
575        ":buffer",
576        ":testing",
577        "//pw_bytes",
578        "//pw_result",
579        "//third_party/fuchsia:stdcompat",
580    ],
581)
582
583pw_cc_test(
584    name = "bump_allocator_test",
585    srcs = [
586        "bump_allocator_test.cc",
587    ],
588    deps = [
589        ":bump_allocator",
590        ":testing",
591        "//pw_unit_test",
592        "//third_party/fuchsia:stdcompat",
593    ],
594)
595
596pw_cc_test(
597    name = "chunk_pool_test",
598    srcs = [
599        "chunk_pool_test.cc",
600    ],
601    deps = [
602        ":chunk_pool",
603        ":testing",
604    ],
605)
606
607pw_cc_test(
608    name = "fallback_allocator_test",
609    srcs = [
610        "fallback_allocator_test.cc",
611    ],
612    deps = [
613        ":fallback_allocator",
614        ":testing",
615        "//pw_status",
616        "//pw_unit_test",
617    ],
618)
619
620pw_cc_test(
621    name = "first_fit_test",
622    srcs = ["first_fit_test.cc"],
623    deps = [
624        ":block_allocator_testing",
625        ":buffer",
626        ":dual_first_fit_block_allocator",
627        ":first_fit",
628        ":first_fit_block_allocator",
629        ":last_fit_block_allocator",
630        "//pw_unit_test",
631        "//third_party/fuchsia:stdcompat",
632    ],
633)
634
635pw_cc_test(
636    name = "fragmentation_test",
637    srcs = ["fragmentation_test.cc"],
638    deps = [
639        ":fragmentation",
640        ":testing",
641        "//pw_unit_test",
642    ],
643)
644
645pw_cc_test(
646    name = "freelist_heap_test",
647    srcs = [
648        "freelist_heap_test.cc",
649    ],
650    deps = [
651        ":freelist_heap",
652        ":testing",
653        "//pw_allocator/block:testing",
654        "//pw_bytes:alignment",
655        "//third_party/fuchsia:stdcompat",
656    ],
657)
658
659pw_cc_test(
660    name = "layout_test",
661    srcs = ["layout_test.cc"],
662    deps = [
663        ":deallocator",
664        ":testing",
665        "//pw_unit_test",
666    ],
667)
668
669pw_cc_test(
670    name = "libc_allocator_test",
671    srcs = [
672        "libc_allocator_test.cc",
673    ],
674    deps = [
675        ":libc_allocator",
676        ":testing",
677        "//pw_unit_test",
678    ],
679)
680
681pw_cc_test(
682    name = "null_allocator_test",
683    srcs = [
684        "null_allocator_test.cc",
685    ],
686    deps = [
687        ":null_allocator",
688        ":testing",
689        "//pw_unit_test",
690    ],
691)
692
693pw_cc_test(
694    name = "pmr_allocator_test",
695    srcs = [
696        "pmr_allocator_test.cc",
697    ],
698    deps = [
699        ":pmr_allocator",
700        ":testing",
701        "//pw_unit_test",
702    ],
703)
704
705pw_cc_test(
706    name = "synchronized_allocator_test",
707    srcs = [
708        "synchronized_allocator_test.cc",
709    ],
710    # TODO: b/358411629 - This test times out on rp2.
711    target_compatible_with = select({
712        "@pico-sdk//bazel/constraint:rp2040": ["@platforms//:incompatible"],
713        "@pico-sdk//bazel/constraint:rp2350": ["@platforms//:incompatible"],
714        "//conditions:default": [],
715    }),
716    deps = [
717        ":synchronized_allocator",
718        ":test_harness",
719        ":testing",
720        "//pw_random",
721        "//pw_sync:binary_semaphore",
722        "//pw_sync:interrupt_spin_lock",
723        "//pw_sync:mutex",
724        "//pw_sync:virtual_basic_lockable",
725        "//pw_thread:test_thread_context",
726        "//pw_thread:thread",
727        "//pw_thread:thread_core",
728        "//pw_thread:yield",
729        "//pw_unit_test",
730    ],
731)
732
733pw_cc_test(
734    name = "tracking_allocator_test",
735    srcs = [
736        "tracking_allocator_test.cc",
737    ],
738    deps = [
739        ":testing",
740        ":tracking_allocator",
741        "//pw_unit_test",
742    ],
743)
744
745pw_cc_test(
746    name = "typed_pool_test",
747    srcs = [
748        "typed_pool_test.cc",
749    ],
750    deps = [
751        ":testing",
752        ":typed_pool",
753        "//pw_bytes:alignment",
754        "//pw_unit_test",
755    ],
756)
757
758pw_cc_test(
759    name = "unique_ptr_test",
760    srcs = ["unique_ptr_test.cc"],
761    deps = [
762        ":allocator",
763        ":testing",
764        "//pw_unit_test",
765    ],
766)
767
768pw_cc_test(
769    name = "worst_fit_test",
770    srcs = ["worst_fit_test.cc"],
771    deps = [
772        ":block_allocator_testing",
773        ":worst_fit",
774        ":worst_fit_block_allocator",
775        "//pw_unit_test",
776    ],
777)
778
779# Docs
780
781cc_library(
782    name = "size_reporter",
783    hdrs = ["public/pw_allocator/size_reporter.h"],
784    strip_include_prefix = "public",
785    deps = [
786        ":null_allocator",
787        "//pw_bloat:bloat_this_binary",
788        "//pw_bytes",
789    ],
790)
791
792filegroup(
793    name = "doxygen",
794    srcs = [
795        "public/pw_allocator/allocator.h",
796        "public/pw_allocator/allocator_as_pool.h",
797        "public/pw_allocator/best_fit.h",
798        "public/pw_allocator/block_allocator.h",
799        "public/pw_allocator/bucket_allocator.h",
800        "public/pw_allocator/buddy_allocator.h",
801        "public/pw_allocator/buffer.h",
802        "public/pw_allocator/bump_allocator.h",
803        "public/pw_allocator/capability.h",
804        "public/pw_allocator/chunk_pool.h",
805        "public/pw_allocator/config.h",
806        "public/pw_allocator/deallocator.h",
807        "public/pw_allocator/fallback_allocator.h",
808        "public/pw_allocator/first_fit.h",
809        "public/pw_allocator/fragmentation.h",
810        "public/pw_allocator/fuzzing.h",
811        "public/pw_allocator/layout.h",
812        "public/pw_allocator/libc_allocator.h",
813        "public/pw_allocator/metrics.h",
814        "public/pw_allocator/null_allocator.h",
815        "public/pw_allocator/pmr_allocator.h",
816        "public/pw_allocator/pool.h",
817        "public/pw_allocator/size_reporter.h",
818        "public/pw_allocator/synchronized_allocator.h",
819        "public/pw_allocator/test_harness.h",
820        "public/pw_allocator/testing.h",
821        "public/pw_allocator/tracking_allocator.h",
822        "public/pw_allocator/typed_pool.h",
823        "public/pw_allocator/unique_ptr.h",
824        "public/pw_allocator/worst_fit.h",
825        "//pw_allocator/block:doxygen",
826        "//pw_allocator/bucket:doxygen",
827    ],
828)
829