forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
alloc_with_zone_interceptor.mm
31 lines (23 loc) · 1.01 KB
/
alloc_with_zone_interceptor.mm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#import "ios/web/alloc_with_zone_interceptor.h"
#import <objc/runtime.h>
#include "base/logging.h"
#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
#endif
namespace web {
void AddAllocWithZoneMethod(Class target, id (^impl_block)(Class, NSZone*)) {
// Make sure |allocWithZone:| is not already implemented in the target class.
Class meta_class = object_getClass(target);
DCHECK_EQ(
class_getMethodImplementation(meta_class, @selector(allocWithZone:)),
class_getMethodImplementation(object_getClass([NSObject class]),
@selector(allocWithZone:)));
IMP new_impl = imp_implementationWithBlock(^(id self, NSZone* zone) {
return impl_block(self, zone);
});
class_addMethod(meta_class, @selector(allocWithZone:), new_impl, "v@:@");
}
} // namespace web