การจัดสรร Arena Header Arena C89 เข้ากันได้
arena.h การจัดสรร Arena เป็นวิธีที่ง่ายในการบรรลุการจัดการหน่วยความจำที่ง่ายขึ้นเร็วขึ้นและปลอดภัยขึ้นโดยการอนุญาตให้มีการจัดสรรหลายครั้งในฐานะกลุ่ม สิ่งนี้ทำได้โดยการจัดสรรหน่วยความจำในภูมิภาคขนาดใหญ่จากนั้นกระจายส่วนของหน่วยความจำนั้นตามต้องการลดปริมาณการโทร malloc (ซึ่งช้าเมื่อเทียบกับเลขคณิตตัวชี้แบบง่าย)
เมื่อคุณทำลายเวทีคุณก็ปลดปล่อยมันและเนื้อหาทั้งหมดลดจำนวนการโทร free ซึ่งช้าเช่นกัน ไปไกลกว่านั้นคุณสามารถเคลียร์ Arenas ได้เพียงแค่รีเซ็ตตัวชี้หน่วยความจำเป็น 0 ทำให้คุณสามารถนำพวกเขากลับมาใช้ใหม่และไม่จำเป็นต้องใช้ malloc และ free มากขึ้น
คุณสามารถเรียนรู้เพิ่มเติมเกี่ยวกับการจัดสรร Arena/Zone/ภูมิภาคโดยการอ่านบทความที่ยอดเยี่ยมนี้
ฉันจะรักษาความสั้นนี้ ฉันยังคงรักษาความสอดคล้อง C89 เพื่อความสนุกสนานไม่ใช่เพราะฉันใช้มัน โดยส่วนตัวแล้วฉันเป็นผู้เพลิดเพลิน C11 ถ้าคุณคิดว่า C89 เป็นวิธีเดียวดีสำหรับคุณ! แต่คุณคิดผิด
เมื่อใดก็ตามที่ฉันแบ่งปันโครงการนี้กับโปรแกรมเมอร์อื่น ๆ หนึ่งในคำตอบที่พบบ่อยที่สุดที่ฉันได้รับคือบางสิ่งบางอย่างตามบรรทัดของ: คุณไม่ควรใส่รหัสการใช้งาน/ลอจิกในไฟล์ส่วนหัว! ฉันมีปัญหากับคำสั่งนี้ด้วยเหตุผลสามประการ ...
มันแสดงให้เห็นว่าการสร้างที่ล้าสมัยและผลที่ตามมานั้นยังคงมีการบังคับใช้ในระบบการศึกษาซึ่งเป็นที่ที่โครงสร้างดังกล่าวมักจะแนะนำ
คนที่ทำคำสั่งนี้ไม่ค่อยมีเหตุผลที่แท้จริงสำหรับการเชื่อ บุคคลนี้เคยคิดหรือไม่ว่า ทำไม พวกเขาควร "ไม่เคยใส่รหัสการใช้งาน/ลอจิกในไฟล์ส่วนหัว"? เพียงแค่สำรอกสิ่งที่พวกเขาได้ยินโดยไม่มีพื้นฐานใด ๆ ที่ พวกเขาเลือกที่จะเห็นด้วยกับมันไม่ได้ช่วยฉันในทางใดทางหนึ่งและบุคคลนี้ไม่ควรคาดหวังให้ฉันยอมรับมันเหมือนที่พวกเขาทำ
การวิพากษ์วิจารณ์ที่ใหญ่ที่สุดและถูกต้องที่สุดของไลบรารีส่วนหัวเท่านั้น/เดี่ยวคือการเปลี่ยนแปลงส่วนหัวนั้นต้องมีการรวบรวมไฟล์ทั้งหมดที่รวมไว้อีกครั้ง ในกรณีของโครงการของฉันการเปลี่ยนแปลง arena.h แม้ว่าการใช้งานจริงจะมีอยู่ในหน่วยการแปลที่ #define ของมาโคร ARENA_IMPLEMENTATION จะส่งผลให้เกิดการสร้างไฟล์ทั้งหมดที่รวมอยู่ด้วย ทางออก? เมื่อ arena.h อยู่ในสถานะที่ต้องการหยุดทำการเปลี่ยนแปลง!
การเชื่อมโยงนั้นเฉื่อยชาและซับซ้อน ผู้เริ่มต้นหลายคนมักจะต่อสู้กับการเรียนรู้กระบวนการเชื่อมโยงและพวกเขายังเป็นผู้ร้ายที่ใหญ่ที่สุดเมื่อพูดถึงการเขียนรหัสที่ไม่ปลอดภัย เพียงอย่างเดียวนี้เป็นเหตุผลที่เพียงพอสำหรับฉันที่จะทำให้สนามกีฬานี้เป็นห้องสมุดส่วนหัวเท่านั้น รหัสของฉัน (ในสถานะปัจจุบัน) มีขนาดเล็กมากประมาณ 300 บรรทัด ทำไมฉันถึงทำให้คุณสร้างและเชื่อมโยงการใช้งานเล็ก ๆ น้อย ๆ เมื่อคุณสามารถ #include มันได้ครั้งเดียวและเริ่มใช้มันนอกกรอบ หากคุณมีปัญหากับมันจริงๆการจัดสรรนี้ในรูปแบบหัวเดี่ยวไม่ได้ป้องกันไม่ให้คุณทำตามโครงสร้างของแหล่งที่มา+หากคุณต้องการ เฮ็ครู้สึกอิสระที่จะแยกมันและทำให้เป็นแหล่งที่มา+ส่วนหัวมันเป็นโอเพ่นซอร์สด้วยเหตุผล!
สิ่งนี้ไม่ได้ใช้งานจัดสรรระดับเคอร์เนล แต่แทนที่จะห่อ malloc และ free (ไลบรารีมาตรฐานหรือกำหนดเองทางเลือกของคุณ)
ในขณะที่ฉันเชื่อว่าซอฟต์แวร์ควรเป็นโอเพ่นซอร์ส แต่ฉันไม่เชื่อว่ามันจะต้องใช้ซอฟต์แวร์ที่ใช้ไลบรารีนี้เพื่อเป็นโอเพนซอร์ส ในยุคสมัยใหม่ของเทคโนโลยีและสถานะปัจจุบันของโลกการเขียนรหัสหน่วยความจำที่ปลอดภัยมีความสำคัญมากกว่าที่เคย ด้วยเหตุผลนี้ซอฟต์แวร์นี้ได้รับอนุญาตภายใต้ Apache License Version 2.0 คุณได้รับการสนับสนุนอย่างยิ่งให้อ่าน LICENSE (รวมอยู่ด้านล่างใน arena.h และไฟล์ของตัวเองใน repo นี้) หากคุณกำลังพิจารณาใช้ซอฟต์แวร์เว้นแต่คุณจะเชื่อว่าคุณคุ้นเคยกับข้อกำหนดและเงื่อนไข 100%
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Copyright 2024 Carter Dugan
เอกสารสำหรับการจัดสรรเวทีทั้งหมดสามารถพบได้ใน arena.h มีความคิดเห็นที่ด้านบนของไฟล์ส่วนหัวพร้อมคำแนะนำอย่างรวดเร็วสำหรับการใช้งานเพื่อความสะดวกของคุณ
สำหรับ ไฟล์หนึ่งไฟล์ในหน่วยแปลเดียว คุณต้องมีสิ่งต่อไปนี้:
#define ARENA_IMPLEMENTATION
#include "arena.h" จากจุดนั้นไฟล์อื่น ๆ ในหน่วยการแปลอื่น ๆ สามารถ #include "arena.h" ตามปกติ มีแมโครเพิ่มเติมที่คุณสามารถกำหนด/ใช้งานได้ ดูส่วนเกี่ยวกับฟังก์ชั่นและมาโคร
มีสองโครงสร้างที่กำหนดไว้ใน arena.h รายการนี้แต่ละรายการพร้อมกับสมาชิก
Arena_Allocation โครงสร้างข้อมูลสำหรับการจัดสรรเวที มีเฉพาะเมื่อมีการกำหนด ARENA_DEBUG
size_t index ดัชนีในเวทีซึ่งจุดเริ่มต้นของการจัดสรรตั้งอยู่size_t size ขนาดของหน่วยความจำที่จัดสรรให้กับการจัดสรรนี้ในไบต์char *pointer ตัวชี้ที่เกี่ยวข้องสำหรับการจัดสรรstruct Arena_Allocation_s *next การจัดสรรถัดไปในรายการที่เชื่อมโยง Arena โครงสร้างข้อมูลสำหรับเวที
char *region ของหน่วยความจำที่จัดสรรsize_t index ดัชนีของภูมิภาคสำหรับตัวชี้ถัดไปที่จะแจกจ่ายsize_t size ขนาดของหน่วยความจำที่จัดสรรให้กับเวทีในไบต์unsigned long allocations จำนวนการจัดสรรเวทีที่ทำ มีเฉพาะเมื่อมีการกำหนด ARENA_DEBUG เท่านั้นArena_Allocation *head_allocation การจัดสรรแรกที่ทำในเวที (ใช้สำหรับรายการที่เชื่อมโยง) มีเฉพาะเมื่อมีการกำหนด ARENA_DEBUG เท่านั้น /*
Allocate and return a pointer to memory to the arena
with a region with the specified size. Providing a
size of zero results in a failure.
Parameters:
size_t size | The size (in bytes) of the arena
memory region.
Return:
Pointer to arena on success, NULL on failure
*/
Arena * arena_create ( size_t size );
/*
Return a pointer to a portion of specified size of the
specified arena's region. Nothing will restrict you
from allocating more memory than you specified, so be
mindful of your memory (as you should anyways) or you
will get some hard-to-track bugs. By default, memory is
aligned by alignof(size_t), but you can change this by
#defining ARENA_DEFAULT_ALIGNMENT before #include'ing
arena.h. Providing a size of zero results in a failure.
Parameters:
Arena *arena | The arena of which the pointer
from the region will be
distributed
size_t size | The size (in bytes) of
allocated memory planned to be
used.
Return:
Pointer to arena region segment on success, NULL on
failure.
*/
ARENA_INLINE void * arena_alloc ( Arena * arena , size_t size );
/*
Same as arena_alloc, except you can specify a memory
alignment for allocations.
Return a pointer to a portion of specified size of the
specified arena's region. Nothing will restrict you
from allocating more memory than you specified, so be
mindful of your memory (as you should anyways) or you
will get some hard-to-track bugs. Providing a size of
zero results in a failure.
Parameters:
Arena *arena | The arena of which the pointer
from the region will be
distributed
size_t size | The size (in bytes) of
allocated memory planned to be
used.
unsigned int alignment | Alignment (in bytes) for each
memory allocation.
Return:
Pointer to arena region segment on success, NULL on
failure.
*/
void * arena_alloc_aligned ( Arena * arena , size_t size , unsigned int alignment );
/*
Copy the memory contents of one arena to another.
Parameters:
Arena *src | The arena being copied, the source.
Arena *dest | The arena being copied to. Must be created/allocated
already.
Return:
Number of bytes copied.
*/
ARENA_INLINE size_t arena_copy ( Arena * dest , Arena * src );
/*
Reset the pointer to the arena region to the beginning
of the allocation. Allows reuse of the memory without
realloc or frees.
Parameters:
Arena *arena | The arena to be cleared.
*/
ARENA_INLINE void arena_clear ( Arena * arena );
/*
Free the memory allocated for the entire arena region.
Parameters:
Arena *arena | The arena to be destroyed.
*/
ARENA_INLINE void arena_destroy ( Arena * arena );
/*
Returns a pointer to the allocation struct associated
with a pointer to a segment in the specified arena's
region.
Parameters:
Arena *arena | The arena whose region should
have a portion pointed to by
ptr.
void *ptr | The ptr being searched for
within the arena in order to
find an allocation struct
associated with it.
*/
Arena_Allocation * arena_get_allocation_struct ( Arena * arena , void * ptr );
/*
Adds an arena allocation to the arena's linked list of
allocations under debug.
Parameters:
Arena *arena | The arena whose allocation list
should be added to
size_t size | The size of the allocation being
added.
*/
void arena_add_allocation ( Arena * arena , size_t size );
/*
Deletes the arena's linked list of allocations under
debug.
Parameters:
Arena *arena | The arena whose allocation list
is being deleted.
*/
void arena_delete_allocation_list ( Arena * arena ); ในรหัสของคุณคุณสามารถกำหนดแมโครเสริมได้ ARENA_MALLOC , ARENA_FREE และ ARENA_MEMCPY สามารถกำหนดให้กับฟังก์ชั่น malloc -like, free -Like และ memcpy ทางเลือกตามลำดับและ arena.h จะใช้ฟังก์ชั่นห้องสมุดมาตรฐาน คุณสามารถเข้าถึงฟังก์ชั่นการดีบักเพิ่มเติมเพื่อติดตามการจัดสรรโดยการกำหนด ARENA_DEBUG ในที่สุดคุณยังสามารถระบุค่าเริ่มต้นสำหรับการจัดตำแหน่งการจัดสรรโดยการกำหนดค่าสำหรับ ARENA_DEFAULT_ALIGNMENT ดูตัวอย่างด้านล่าง
// All of these are optional
// Replace standard library functions
#define ARENA_MALLOC <stdlib_malloc_like_allocator>
#define ARENA_FREE <stdlib_free_like_deallocator>
#define ARENA_MEMCPY <stdlib_memcpy_like_copier>
// for debug functionality:
#define ARENA_DEBUG
// If you would like to change the default alignment for
// allocations:
#define ARENA_DEFAULT_ALIGNMENT <alignment_value> นอกจากนี้ยังมีมาโครสำหรับการกำหนดประเภทของประเภท เช่นเดียวกับทุกสิ่งทุกอย่างมันก็เป็นมิตรกับ C89 แม้ว่าเมื่อรวบรวมภายใต้ C11 มันจะใช้ alignof ของ stdalign.h
ARENA_ALIGNOF ( type ) // Gives alignment of `type` รหัสถูกเขียนขึ้นเพื่อสร้างด้วยคอมไพเลอร์ใด ๆ ที่รองรับมาตรฐาน C89 และทำงานบนแพลตฟอร์มใด ๆ อย่างไรก็ตามมีปัญหาบางอย่างเกี่ยวกับการสร้างบนหน้าต่างด้วย Makefile โปรดอ่านข้อมูลด้านล่างทั้งหมด
การทดสอบและตัวอย่างได้รับการรวบรวมและทำงานได้สำเร็จภายใต้คอมไพเลอร์และเวอร์ชันต่อไปนี้:
การทดสอบและตัวอย่างถูกรวบรวมและทำงานได้สำเร็จในระบบปฏิบัติการต่อไปนี้:
หมายเหตุ ในปัจจุบัน MakeFile ยังไม่ได้รับการกำหนดค่าให้ทำงานบน Windows เมื่อเรียกใช้ $ make test เนื่องจากการใช้ Valgrind และการขาดส่วนขยาย .exe นี่ควรเป็นการแก้ไขที่ง่ายและอยู่ในรายการสิ่งที่ต้องทำของฉัน (อย่าลังเลที่จะเปิดปัญหาและแก้ไขด้วยตัวเอง!)
โครงการนี้มีแนวทางง่าย ๆ สำหรับการมีส่วนร่วม
สำหรับ การมีส่วนร่วมใด ๆ ในรหัสของ arena.h โปรดเปิดคำขอดึงเฉพาะในกรณีที่คุณกำลัง แก้ไขปัญหา เพื่อตอบสนอง คำขอคุณสมบัติหรือแก้ไขข้อผิดพลาด ทำตามรูปแบบรหัสและเรียกใช้การทดสอบอัตโนมัติก่อนที่จะเปิดคำขอดึง การทดสอบทั้งหมดจะต้องผ่าน
สำหรับ คำขอ หรือ ข้อบกพร่อง โปรด เปิดปัญหา จากนั้นคุณสามารถแก้ไขปัญหานี้ด้วยตัวเอง
สำหรับ สิ่งอื่นใด ให้เปิดปัญหาและเราจะพูดคุยกัน
arena.hในขณะนี้ไม่มีเอกสารสำหรับสไตล์รหัส แต่ควรจะค่อนข้างง่ายพอที่จะรับผ่านการอ่านรหัสที่มีอยู่สำหรับสิ่งต่าง ๆ ส่วนใหญ่ หากคุณมีปัญหาอย่าลังเลที่จะเปิดปัญหาสำหรับ FR หากมีอยู่แล้วให้แสดงความคิดเห็นเกี่ยวกับสิ่งที่คุณสับสน
หากคุณแก้ไข arena.h ใด ๆ คุณต้องเรียกใช้การทดสอบ ดูส่วนถัดไป
หากคุณเพิ่มคุณสมบัติภายใน arena.h คุณต้องสร้างการทดสอบหรือการทดสอบที่เพียงพอภายใน test.c
หากคุณเพิ่มคุณสมบัติภายใน arena.h คุณต้อง มี ตัวอย่างที่เพียงพอใน code_examples/ และ เพิ่มลงใน makefile แต่ไม่จำเป็น
หากคุณเปลี่ยน arena.h ใด ๆ ให้ทำการทดสอบก่อนที่จะเปิดการประชาสัมพันธ์ หากคุณเปิดการประชาสัมพันธ์ด้วยการปรับเปลี่ยนรหัสและการทดสอบไม่ผ่านทั้งหมดให้แสดงความคิดเห็นเกี่ยวกับการระบุว่าการทดสอบที่คุณเชื่อว่าผิดและป้องกันไม่ให้คุณผ่านการทดสอบทั้งหมด หากการทดสอบใด ๆ ล้มเหลวและการประชาสัมพันธ์ของคุณไม่มีความคิดเห็นที่เรียกร้องให้แก้ไขการทดสอบที่ล้มเหลว PR ของคุณจะถูกละเว้นปิด
นอกเหนือจากการระบุข้อบกพร่องและคำขอคุณสมบัติแล้วการตอบสนองคำขอคุณลักษณะหรือการแก้ไขข้อบกพร่องสำหรับฟังก์ชันการทำงานภายใน arena.h อนุญาตให้แก้ไขหรือเพิ่มรหัสทดสอบที่เกี่ยวข้องภายใน test.c และคุณต้องทำเช่นนั้นหากคุณต้องการให้ PR ของคุณได้รับการยอมรับ มีเอกสารสำหรับรหัสทดสอบภายใน test.c ที่ด้านบนของไฟล์ในรูปแบบของความคิดเห็น
การทดสอบจะต้องผ่านการรั่วไหลของ Valgrind และ arena.h ต้อง เป็นไปตามมาตรฐาน C89 คุณควรตรวจสอบสิ่งนี้โดยใช้ Makefile แต่ถ้าด้วยเหตุผลบางอย่างคุณไม่สามารถหรือไม่ต้องการรวบรวม test.c ด้วย
-Werror -Wall -Wextra -Wstrict-prototypes -Wold-style-definition -Wmissing-prototypes -Wmissing-declarations -Wdeclaration-after-statement
และสำหรับการปฏิบัติตามกฎระเบียบคอมไพล์ test_compliance.c ด้วย
-pedantic -std=c89 -Werror -Wall -Wextra -Wstrict-prototypes -Wold-style-definition -Wmissing-prototypes -Wmissing-declarations -Wdeclaration-after-statement
อย่างที่ฉันพูดคุณสามารถทำสิ่งเหล่านี้ทั้งหมดด้วย Makefile
$ make test
ตัวระบุ
variable_name สำหรับ พอยน์เตอร์ ควรติดตั้งเครื่องหมายดอกจัน ( * ) กับชื่อตัวแปรไม่ใช่ประเภทเช่น type *variable_nametype function_name(p1, p2, ...) ในกรณีของพอยน์เตอร์เครื่องหมายดอกจัน ( * ) ควรติดอยู่กับประเภทสำหรับความหนาแน่นเช่น type* function_name(p1, p2, ...) ฟังก์ชั่นควรมีการประกาศไปข้างหน้าพร้อมความคิดเห็นสำหรับเอกสารด้านบนรอบด้านบนของไฟล์ตรวจสอบให้แน่ใจว่าคำสั่งที่เกี่ยวข้องกับฟังก์ชั่นอื่น ๆ นั้นสอดคล้องกันstruct StructName พวกเขาควรพิมพ์และตั้งอยู่รอบด้านบนของไฟล์ส่วนหัวARENA_ เช่น ARENA_MACRO_NAME โปรดหลีกเลี่ยงการเพิ่มมาโครโดยไม่ปรึกษาฉันก่อน อย่าลังเลที่จะเปลี่ยนแปลงมาโครที่มีอยู่White Space ขึ้นอยู่กับความเกี่ยวข้องของบรรทัดของรหัสกับคนรอบข้าง หากคุณไม่เข้าใจว่าประเด็นเหล่านี้หมายถึงอะไรโปรดดูรหัส ควรจัดรูปแบบดังนี้:
การตรวจสอบข้อผิดพลาด ควรทำเมื่อใดก็ตามที่เป็นไปได้และเลียนแบบพฤติกรรมของการใช้งานไลบรารีมาตรฐานเช่นการส่งคืน NULL ในข้อผิดพลาดในฟังก์ชั่นที่ส่งคืนพอยน์เตอร์หรือส่งคืนค่าความผิดพลาดจำนวนเต็มจากฟังก์ชั่นจำนวนเต็ม คุณควรใช้ การตรวจสอบข้อผิดพลาดก่อน ซึ่งหมายถึงการตรวจสอบข้อผิดพลาดทันทีที่สามารถผลิตได้เช่น การตรวจสอบการส่งคืน NULL หลังจากการโทร malloc ที่ล้มเหลว
ความคิดเห็น ควรอธิบาย ว่าทำไม คุณถึงทำอะไรบางอย่างไม่ใช่ สิ่ง ที่คุณทำ กล่าวอีกนัยหนึ่งรหัสของคุณควรอธิบายตนเอง เอกสารสำหรับฟังก์ชั่นในรูปแบบของความคิดเห็นควรอยู่เหนือฟังก์ชั่นในรูปแบบต่อไปนี้:
/*
Description of function, description of function description of function.
Description of function description of function, description of function
description of function.
Parameters:
paramter1_type paramter1_name | Description of parameter 1, description
of parameter one description of paramter
1.
paramter2_type paramter2_name | Description of parameter 2, description
of parameter one description of paramter
2.
Return:
Description of return value, description of return value description of return value.
*/