واجهة المخصّص
عقد واحد لجميع مصادر الذاكرة
واجهة المخصّص درس مجاني في Zig Academy على CoddyKit. هذا هو الدرس 2 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Zig Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Zig Academy 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
One Common Type
Every allocator in Zig is handed around as a single type, std.mem.Allocator. Your code talks to that one interface, never a concrete backend.
const Allocator = std.mem.Allocator;An Interface, Not a Class
std.mem.Allocator is a small interface: a pointer plus a table of functions. It describes what an allocator can do, not which one you use.
Many Backends, One Shape
A page allocator, an arena, or a test allocator all expose the same shape. Swapping one for another needs no change to calling code.
Get a Real Allocator
You obtain a concrete allocator, then take its .allocator() handle to pass around as the generic std.mem.Allocator type.
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
const a = gpa.allocator();Pass It Down
Once you hold an allocator, you pass that one value into any function that needs memory. The whole program can share a single source.
try doWork(a);The Page Allocator
std.heap.page_allocator asks the OS directly for memory. It is simple but coarse, so it suits big or long-lived blocks, not tiny ones.
const a = std.heap.page_allocator;GeneralPurposeAllocator
The GeneralPurposeAllocator is the everyday choice in debug builds. It tracks every block and reports leaks and double-frees for you.
Why an Interface Helps
Coding against the interface means your function works with any allocator. The same code runs in tests, on bare metal, or in a server.
Functions Just Ask
A function does not care which allocator it got. It simply calls methods like alloc and free on the std.mem.Allocator it was given.
const buf = try a.alloc(u8, 16);No Global Allocator
Zig has no single global heap. You decide which allocator each part of your program uses, keeping memory sources visible and local.
Test with a Different One
In tests you pass std.testing.allocator instead. Same code, but now leaks fail the test, proving the interface pays off. ✅
Quick Check
Think about what type all Zig allocators share.
Recap
You learned that std.mem.Allocator is one interface over many backends, so code that asks for memory works with any allocator you supply. 🎯
الأسئلة الشائعة
هل درس «واجهة المخصّص» مجاني؟
نعم — نص درس «واجهة المخصّص» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Zig Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Zig Academy 4 دروس في المجموع.
ماذا ستتعلم في «واجهة المخصّص»؟
عقد واحد لجميع مصادر الذاكرة تتمرن على Zig Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Zig Academy؟
لا تُشترط خبرة سابقة. Zig Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 2 من أصل 4.
كم من الوقت يستغرق درس «واجهة المخصّص»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Zig Academy هذا؟
نعم. كل درس في Zig Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- لماذا لا يحتوي Zig على تخصيصات خفية
- واجهة المخصّص
- alloc وfree وcreate وdestroy
- القوائم الديناميكية باستخدام ArrayList