--- nono/vm/ethernet.cpp 2026/04/29 17:04:28 1.1 +++ nono/vm/ethernet.cpp 2026/04/29 17:04:35 1.1.1.3 @@ -1,16 +1,17 @@ // // nono -// Copyright (C) 2019 isaki@NetBSD.org +// Copyright (C) 2020 nono project +// Licensed under nono-license.txt // -#include "configfile.h" #include "ethernet.h" +#include "config.h" #include "netdriver_bpf.h" #include "netdriver_tap.h" #include "netdriver_none.h" #include -EthernetDevice *gEthernet; +std::unique_ptr gEthernet; // コンストラクタ EthernetDevice::EthernetDevice() @@ -20,49 +21,45 @@ EthernetDevice::EthernetDevice() // デストラクタ EthernetDevice::~EthernetDevice() { - if (gNetDriver) { - delete gNetDriver; - gNetDriver = NULL; - } } // 動的なコンストラクション bool EthernetDevice::Create() { - const std::string key = "ethernet.hostdriver"; - const std::string type = gConfig->ReadStr(key, "none"); + const ConfigItem& item = gConfig->Find("ethernet-hostdriver"); + const std::string& type = item.AsString(); if (type == "none") { - gNetDriver = new NetDriverNone(); + gNetDriver.reset(new NetDriverNone()); } else if (type == "tap") { #if defined(NETDRIVER_TAP) try { - gNetDriver = new NetDriverTap(this, &mtx, &cv); + gNetDriver.reset(new NetDriverTap(this, &mtx, &cv)); } catch (...) { warnx("Creating NetDriverTap failed"); return false; } #else - gConfig->Err(key, "\"%s\" not supported", type.c_str()); + item.Err("tap not supported"); return false; #endif } else if (type == "bpf") { #if defined(NETDRIVER_BPF) try { - gNetDriver = new NetDriverBPF(this, &mtx, &cv); + gNetDriver.reset(new NetDriverBPF(this, &mtx, &cv)); } catch (...) { warnx("Creating NetDriverBPF failed"); return false; } #else - gConfig->Err(key, "\"%s\" not supported", type.c_str()); + item.Err("bpf not supported"); return false; #endif } else { - gConfig->Err(key, "unknown \"%s\"", type.c_str()); + item.Err(); return false; } - assert(gNetDriver); + assert((bool)gNetDriver); return true; } @@ -72,8 +69,7 @@ bool EthernetDevice::Init() { if (gNetDriver->Init() == false) { - delete gNetDriver; - gNetDriver = NULL; + gNetDriver.reset(); return false; }